Discuss Scratch
- Discussion Forums
- » Advanced Topics
- » How To Optimize Pen?
- MegaApuTurkUltra
-
Scratcher
1000+ posts
How To Optimize Pen?
Note: This is a Question About Scratch but I'm asking here because ATers are more likely to know the answer 
So I've been working on this project: https://scratch.mit.edu/projects/89337582/
What I've noticed is that contrary to my beliefs, giant list operations are basically instant while every pen stroke costs me an enormous amount of time.
So I thought I'd ask you ATers who've done stuff with pen how to optimize pen operations?
Basically the system I have now for that is that it draws the outline of the curvy shape you see, and then it goes around and draws a bunch of radial lines to fill it in.
Now I could switch it so that to fill it in, it goes around the shape with pen drawing curvy things instead of straight lines. My questions are:
- Is drawing a few curvy lines better that drawing a lot of straight lines?
- Should I be aiming to reduce the number of pen up/pen downs?
- How many pen strokes can I draw while maintaining a solid FPS?

So I've been working on this project: https://scratch.mit.edu/projects/89337582/
What I've noticed is that contrary to my beliefs, giant list operations are basically instant while every pen stroke costs me an enormous amount of time.
So I thought I'd ask you ATers who've done stuff with pen how to optimize pen operations?
Basically the system I have now for that is that it draws the outline of the curvy shape you see, and then it goes around and draws a bunch of radial lines to fill it in.
Now I could switch it so that to fill it in, it goes around the shape with pen drawing curvy things instead of straight lines. My questions are:
- Is drawing a few curvy lines better that drawing a lot of straight lines?
- Should I be aiming to reduce the number of pen up/pen downs?
- How many pen strokes can I draw while maintaining a solid FPS?
- Zro716
-
Scratcher
1000+ posts
How To Optimize Pen?
I heard DadOfMrLog griffpatch (oops) optimizes his pen projects by placing
set [color v] effect to (0)to trigger the Stage3D player which has slightly faster drawing.
Last edited by Zro716 (Nov. 29, 2015 06:13:25)
- MegaApuTurkUltra
-
Scratcher
1000+ posts
How To Optimize Pen?
I heard DadOfMrLog optimizes his pen projects by placingReally? Interesting.set [color v] effect to (0)to trigger the Stage3D player which has slightly faster drawing.
I try to avoid stage3D wherever possible because it causes tons of lag

Edit: It gave me an extra 3 fps. Yay.
Last edited by MegaApuTurkUltra (Nov. 28, 2015 18:32:46)
- comp09
-
Scratcher
1000+ posts
How To Optimize Pen?
Your speed boost will depend on your system configuration. You won't get anything if your system doesn't support stage3d, and slow GPUs might make it worse.I heard DadOfMrLog optimizes his pen projects by placingReally? Interesting.set [color v] effect to (0)to trigger the Stage3D player which has slightly faster drawing.
I try to avoid stage3D wherever possible because it causes tons of lag
Edit: It gave me an extra 3 fps. Yay.
- DadOfMrLog
-
Scratcher
1000+ posts
How To Optimize Pen?
I heard DadOfMrLog optimizes his pen projects by…Wow, that's interesting, I'd not heard he did that…
Oh…
Wait…
No, I've never done that. In fact, I've tended to avoid Stage3D because of its glitches - although many have now been fixed, so it's way better than it was. (The main ones now, I think, are the stretching of large costumes / text and the brightness effect using HSV rather than HSL, as well as the inherent fuzziness/pixellation that you get when zooming up a vector costume that has effects applied).
It's true, though, that many things are quicker in Stage3D - it certainly tends to deal with costumes much quicker (switch costume, rotate, effects, resize, etc.) I haven't benchmarked differences with pen, but I can believe it might be a bit quicker with Stage3D.
But going back to your original question… the key things I've found when using pen are:
- draw as few lines as possible (i.e. you shouldn't make curves too accurate);
- cover as much area with each line as you can (which is related to above);
- try to reduce overlapping areas of pen drawing (overlapping pen causes a performance hit in Flash);
- if you do have lots of pen drawing in a frame, and especially if there's a fair bit of pen overlap, then stamp a blank costume every couple of hundred lines/dots or so (this commits the pen-strokes to the canvas, which reduces the performance hit with overlapping pen);
- horizontal (and vertical?) lines are much quicker to draw than non-horiz(/vert);
- every “pen down” you do draws a dot at the current pen-size, so draw as many lines as you can for each “pen down” (drawing that dot has a performance hit, since it overlaps the line you're about to draw next);
- if your ratio of pen-downs to lines drawn is quite high (maybe more than 1 in 4), and your lines are more than a couple of pixels wide (which they should be if trying to fulfil the second point as much as possible), then set the pen-size to 1 before pen-down so it only draws a very small dot at pen-down, then set it to the size you want before drawing the line(s);
- I guess I could also add that drawing with alpha is somewhat slower than no alpha (though I think that'd be fairly obvious).
Hope those are helpful!
Last edited by DadOfMrLog (Nov. 29, 2015 02:27:13)
- NickyNouse
-
Scratcher
1000+ posts
How To Optimize Pen?
This probably isn't much help, for all I know the slowness came from somewhere else, but I've found that there can be a huge slowdown when trying to draw out of the stage's bounds, especially in no-refresh scripts, so take care to stop your lines before you hit the edge
Last edited by NickyNouse (Nov. 29, 2015 02:29:26)
- gdpr533f604550b2f20900645890
-
Scratcher
1000+ posts
How To Optimize Pen?
Griffpatch said that, not you.I heard DadOfMrLog optimizes his pen projects by…Wow, that's interesting, I'd not heard he did that…
- MegaApuTurkUltra
-
Scratcher
1000+ posts
How To Optimize Pen?
I heard DadOfMrLog optimizes his pen projects by…Wow, that's interesting, I'd not heard he did that…
Oh…
Wait…
No, I've never done that. In fact, I've tended to avoid Stage3D because of its glitches - although many have now been fixed, so it's way better than it was. (The main ones now, I think, are the stretching of large costumes / text and the brightness effect using HSV rather than HSL, as well as the inherent fuzziness/pixellation that you get when zooming up a vector costume that has effects applied).
It's true, though, that many things are quicker in Stage3D - it certainly tends to deal with costumes much quicker (switch costume, rotate, effects, resize, etc.) I haven't benchmarked differences with pen, but I can believe it might be a bit quicker with Stage3D.
But going back to your original question… the key things I've found when using pen are:
- draw as few lines as possible (i.e. you shouldn't make curves too accurate);
- cover as much area with each line as you can (which is related to above);
- try to reduce overlapping areas of pen drawing (overlapping pen causes a performance hit in Flash);
- if you do have lots of pen drawing in a frame, and especially if there's a fair bit of pen overlap, then stamp a blank costume every couple of hundred lines/dots or so (this commits the pen-strokes to the canvas, which reduces the performance hit with overlapping pen);
- horizontal (and vertical?) lines are much quicker to draw than non-horiz(/vert);
- every “pen down” you do draws a dot at the current pen-size, so draw as many lines as you can for each “pen down” (drawing that dot has a performance hit, since it overlaps the line you're about to draw next);
- if your ratio of pen-downs to lines drawn is quite high (maybe more than 1 in 4), and your lines are more than a couple of pixels wide (which they should be if trying to fulfil the second point as much as possible), then set the pen-size to 1 before pen-down so it only draws a very small dot at pen-down, then set it to the size you want before drawing the line(s);
- I guess I could also add that drawing with alpha is somewhat slower than no alpha (though I think that'd be fairly obvious).
Hope those are helpful!
Thanks for the help

I managed to double performance by drawing the entire thing in one pen down and using a larger pen size. Instead of radial lines I used a thick pen to draw the outline of the shape. Then the sprite I have in the center covers up what's left unfilled.
You can see the project here: https://scratch.mit.edu/projects/89337582/
- DadOfMrLog
-
Scratcher
1000+ posts
How To Optimize Pen?
I managed to double performance…Yup, it's running fairly well on my MacBook. I suspect some slower machines will struggle, though (certainly with “fancy” on).
I did try it cut down from drawing 80 lines around to only 40, though, and TBH, there's hardly any noticeable degradation in the visual quality, but it certainly makes it faster - you might want to consider cutting it down to something like that.
Last edited by DadOfMrLog (Nov. 30, 2015 00:31:34)
- DadOfMrLog
-
Scratcher
1000+ posts
How To Optimize Pen?
Interestingly, I don't see any noticeable difference between Stage3D vs normal player on my MacBook for that project.Griffpatch said that, not you.I heard DadOfMrLog optimizes his pen projects by…Wow, that's interesting, I'd not heard he did that…
I suspect comp09 is correct and that it may depend on system config (graphics card, etc.)
Also…
Edit: It gave me an extra 3 fps. Yay.I see a fisheye effect in there too (on the stage) - if that was there before you added the “set color effect” block, wouldn't that mean you were already using Stage3D…?
Last edited by DadOfMrLog (Nov. 30, 2015 00:30:22)
- MegaApuTurkUltra
-
Scratcher
1000+ posts
How To Optimize Pen?
That was the last thing I added, after I saw the performance increase from using larger pen strokes. I know how stage3d worksInterestingly, I don't see any noticeable difference between Stage3D vs normal player on my MacBook for that project.Griffpatch said that, not you.I heard DadOfMrLog optimizes his pen projects by…Wow, that's interesting, I'd not heard he did that…
I suspect comp09 is correct and that it may depend on system config (graphics card, etc.)
Also…Edit: It gave me an extra 3 fps. Yay.I see a fisheye effect in there too (on the stage) - if that was there before you added the “set color effect” block, wouldn't that mean you were already using Stage3D…?

- liam48D
-
Scratcher
1000+ posts
How To Optimize Pen?
nah matu is lieing hes a n00bThat was the last thing I added, after I saw the performance increase from using larger pen strokes. I know how stage3d worksInterestingly, I don't see any noticeable difference between Stage3D vs normal player on my MacBook for that project.Griffpatch said that, not you.I heard DadOfMrLog optimizes his pen projects by…Wow, that's interesting, I'd not heard he did that…
I suspect comp09 is correct and that it may depend on system config (graphics card, etc.)
Also…Edit: It gave me an extra 3 fps. Yay.I see a fisheye effect in there too (on the stage) - if that was there before you added the “set color effect” block, wouldn't that mean you were already using Stage3D…?
jk- NickyNouse
-
Scratcher
1000+ posts
How To Optimize Pen?
nah matu is lieing hes a n00bThat was the last thing I added, after I saw the performance increase from using larger pen strokes. I know how stage3d worksInterestingly, I don't see any noticeable difference between Stage3D vs normal player on my MacBook for that project.Griffpatch said that, not you.I heard DadOfMrLog optimizes his pen projects by…Wow, that's interesting, I'd not heard he did that…
I suspect comp09 is correct and that it may depend on system config (graphics card, etc.)
Also…Edit: It gave me an extra 3 fps. Yay.I see a fisheye effect in there too (on the stage) - if that was there before you added the “set color effect” block, wouldn't that mean you were already using Stage3D…?
- TheLogFather
-
Scratcher
1000+ posts
How To Optimize Pen?
…Oh, wow… in that case, it'd be kinda handy if you could you fix the brightness bug, please.
I know how stage3d works
^2I've been meaning to work through the coding to see if I could figure it out, but the AGAL code it uses is not exactly easy on the eyes (I always find something else I'd rather do…)
Last edited by TheLogFather (Dec. 1, 2015 22:08:55)
- Jonathan50
-
Scratcher
1000+ posts
How To Optimize Pen?
btw…Oh, wow… in that case, it'd be kinda handy if you could you fix the brightness bug, please.
I know how stage3d works^2
I've been meaning to work through the coding to see if I could figure it out, but the AGAL code it uses is not exactly easy on the eyes (I always find something else I'd rather do…)
^2 = 

- MegaApuTurkUltra
-
Scratcher
1000+ posts
How To Optimize Pen?
I meant that I know how stage3d mode in the player works (eg I understand how you trigger it). I could probably fix that bug though given enough time…Oh, wow… in that case, it'd be kinda handy if you could you fix the brightness bug, please.
I know how stage3d works^2
I've been meaning to work through the coding to see if I could figure it out, but the AGAL code it uses is not exactly easy on the eyes (I always find something else I'd rather do…)

- TheLogFather
-
Scratcher
1000+ posts
How To Optimize Pen?
I meant that I know how stage3d mode in the player works (eg I understand how you trigger it). I could probably fix that bug though given enough timeYeah, I knew what you meant - was just my vague attempt at some sort of off-topic humour (since this seems to be the forum for such…)
The AGAL code itself is clearly using a standard RGB->HSV algorithm, then a simple modification for colour & brightness effects, then standard HSV->RGB, so it's really obvious it's doing the wrong thing.
It just needs all that ripping out (lines 191 to 277) and ‘simply’ rewriting to do RGB->HSL instead, keeping the colour/brightness modification as is, then do HSL->RGB instead. But writing that AGAL is so tedious ‘cos of the limitations on what you can do, and how hard it is to read (not to mention that I have no prior experience with AGAL, though I suppose it’s a little like assembly).
Last edited by TheLogFather (Dec. 3, 2015 14:59:54)
- liam48D
-
Scratcher
1000+ posts
How To Optimize Pen?
Yup.I meant that I know how stage3d mode in the player works (eg I understand how you trigger it). I could probably fix that bug though given enough timeYeah, I knew what you meant - was just my vague attempt at some sort of off-topic humour (since this seems to be the forum for such…)

- fnafsher187
-
Scratcher
30 posts
How To Optimize Pen?
my ray caster is still at 1.2 fps.
the distances go into a list which then goes to a sprite that draws it in one stroke.
the distances go into a list which then goes to a sprite that draws it in one stroke.
Last edited by fnafsher187 (June 20, 2022 17:40:11)
- Discussion Forums
- » Advanced Topics
-
» How To Optimize Pen?