There are 6 canvases on the page:
imageCanvas
is the only visible canvas. It's where the composite image is rendered and is downloaded when the user is done.tempCanvas
holds path data when drawing paths.holderCanvas
takes a snapshot of theimageCanvas
as soon as a new path starts.rotationCanvas
blurredCanvas
holds a blurred version of the current image, re-blurred on each mouseup.offscreenCanvas
, which is used to render the custom cursor.
When you open an image:
- All canvases are resized to match the image's dimensions, scaled so that no side is 2500px or more.
- The image is drawn on
imageCanvas
androtationCanvas
. - The brush size and blur radius adjust themselves to the size of the image - the larger the image, the larger the brush and blur.
- The cursor is rendered offscreen and saved to a data URL in CSS.
When you click or tap on the canvas:
- On mouse down/tap start:
- The current
imageCanvas
image is copied to theholderCanvas
. - The
tempCanvas
is cleared. - The mouse/touch position is saved as
lastPos
.
- The current
- On mouse/touch move:
- A circle is drawn at each point to the
imageCanvas
andtempCanvas
. - A rectangle is interpolated between the new position and
lastPos
. - The mouse/touch position is saved as
lastPos
.
- A circle is drawn at each point to the
- On mouse/tap end (or the cursor leaving the canvas):
- If the user is painting a solid color:
- No extra action is taken, since the solid colors were drawn onto the
imageCanvas
during the mouse/tap move step.
- No extra action is taken, since the solid colors were drawn onto the
- If the user is painting a blur:
- The image from the
rotationCanvas
is copied to theblurredCanvas
. - The
blurredCanvas
image is pixelated (pixelateCanvas
), then blurred (stackBlurCanvasRGBA
). - The image from the
blurredCanvas
is drawn onto thetempCanvas
, using the alpha from the existingtempCanvas
image as a mask (source-in composite operation). This creates a blurred image that matches the path drawn on mouse/touch move. - The image from the
tempCanvas
is drawn onto theimageCanvas
. - The image from the
holderCanvas
is drawn onto theimageCanvas
below the existing content.
- The image from the
- If the user is painting an undo:
- The same steps as the blur apply, but with a blur radius of 0 (ie, the unmodified image).
- If the user is painting a solid color:
When you select "Rotate Image":
1. `imageCanvas`, `tempCanvas`, `rotationCanvas`, `blurredCanvas` are resized, translated, and rotated.