import paper from '@turbowarp/paper'; import {createCanvas, clearRaster, getRaster, hideGuideLayers, showGuideLayers} from './layer'; import {getGuideColor} from './guides'; import {clearSelection} from './selection'; import {ART_BOARD_WIDTH, ART_BOARD_HEIGHT, CENTER, MAX_WORKSPACE_BOUNDS} from './view'; import Formats from '../lib/format'; import log from '../log/log'; /** * @param {string|CanvasGradient} color The canvas's fillStyle. * @returns {boolean} True if the style will require using a mask to draw. */ const doesColorRequireMask = color => ( color instanceof CanvasGradient || color.startsWith('rgba(') || (color.startsWith('#') && color.length > 7) ); const createMaskingCanvas = (originalContext, fillStyle) => { const originalCanvas = originalContext.canvas; if (fillStyle === null) { fillStyle = '#00000000'; } if (doesColorRequireMask(fillStyle)) { const tempCanvas = createCanvas(originalCanvas.width, originalCanvas.height); const tempContext = tempCanvas.getContext('2d', { willReadFrequently: true }); return { context: tempContext, unmask: () => { tempContext.globalCompositeOperation = 'source-in'; tempContext.fillStyle = fillStyle; tempContext.fillRect(0, 0, originalCanvas.width, originalCanvas.height); originalContext.drawImage(tempCanvas, 0, 0); } }; } // Simple colors do not require any tricks to draw. originalContext.fillStyle = fillStyle; return { context: originalContext, unmask: () => {} }; }; const forEachLinePoint = function (point1, point2, callback) { // Bresenham line algorithm let x1 = ~~point1.x; const x2 = ~~point2.x; let y1 = ~~point1.y; const y2 = ~~point2.y; const dx = Math.abs(x2 - x1); const dy = Math.abs(y2 - y1); const sx = (x1 < x2) ? 1 : -1; const sy = (y1 < y2) ? 1 : -1; let err = dx - dy; callback(x1, y1); while (x1 !== x2 || y1 !== y2) { const e2 = err * 2; if (e2 > -dy) { err -= dy; x1 += sx; } if (e2 < dx) { err += dx; y1 += sy; } callback(x1, y1); } }; /** * @param {!number} a Coefficient in ax^2 + bx + c = 0 * @param {!number} b Coefficient in ax^2 + bx + c = 0 * @param {!number} c Coefficient in ax^2 + bx + c = 0 * @return {Array} Array of 2 solutions, with the larger solution first */ const solveQuadratic_ = function (a, b, c) { const soln1 = (-b + Math.sqrt((b * b) - (4 * a * c))) / 2 / a; const soln2 = (-b - Math.sqrt((b * b) - (4 * a * c))) / 2 / a; return soln1 > soln2 ? [soln1, soln2] : [soln2, soln1]; }; /** * @param {!object} options drawing options * @param {!number} options.centerX center of ellipse, x * @param {!number} options.centerY center of ellipse, y * @param {!number} options.radiusX major radius of ellipse * @param {!number} options.radiusY minor radius of ellipse * @param {!number} options.shearSlope slope of the sheared x axis * @param {?boolean} options.isFilled true if isFilled * @param {?function} options.drawFn The function called on each point in the outline, used only * if isFilled is false. * @param {!CanvasRenderingContext2D} context for drawing * @return {boolean} true if anything was drawn, false if not */ const drawShearedEllipse_ = function (options, context) { const centerX = ~~options.centerX; const centerY = ~~options.centerY; const radiusX = ~~Math.abs(options.radiusX) - .5; const radiusY = ~~Math.abs(options.radiusY) - .5; const shearSlope = options.shearSlope; const isFilled = options.isFilled; const drawFn = options.drawFn; if (shearSlope === Infinity || radiusX < 1 || radiusY < 1) { return false; } // A, B, and C represent Ax^2 + Bxy + Cy^2 = 1 coefficients in a skewed ellipse formula const A = (1 / radiusX / radiusX) + (shearSlope * shearSlope / radiusY / radiusY); const B = -2 * shearSlope / radiusY / radiusY; const C = 1 / radiusY / radiusY; // Line with slope1 intersects the ellipse where its derivative is 1 const slope1 = ((-2 * A) - B) / ((2 * C) + B); // Line with slope2 intersects the ellipse where its derivative is -1 const slope2 = (-(2 * A) + B) / (-(2 * C) + B); const verticalStepsFirst = slope1 > slope2; /** * Vertical stepping portion of ellipse drawing algorithm * @param {!number} startY y to start drawing from * @param {!function} conditionFn function which should become true when we should stop stepping * @return {object} last point drawn to the canvas, or null if no points drawn */ const drawEllipseStepVertical_ = function (startY, conditionFn) { // Points on the ellipse let y = startY; let x = solveQuadratic_(A, B * y, (C * y * y) - 1); // last pixel position at which a draw was performed let pY; let pX1; let pX2; while (conditionFn(x[0], y)) { pY = Math.floor(y); pX1 = Math.floor(x[0]); pX2 = Math.floor(x[1]); if (isFilled) { context.fillRect(centerX - pX1 - 1, centerY + pY, pX1 - pX2 + 1, 1); context.fillRect(centerX + pX2, centerY - pY - 1, pX1 - pX2 + 1, 1); } else { drawFn(centerX - pX1 - 1, centerY + pY); drawFn(centerX + pX1, centerY - pY - 1); } y--; x = solveQuadratic_(A, B * y, (C * y * y) - 1); } return pX1 || pY ? {x: pX1, y: pY} : null; }; /** * Horizontal stepping portion of ellipse drawing algorithm * @param {!number} startX x to start drawing from * @param {!function} conditionFn function which should become false when we should stop stepping * @return {object} last point drawn to the canvas, or null if no points drawn */ const drawEllipseStepHorizontal_ = function (startX, conditionFn) { // Points on the ellipse let x = startX; let y = solveQuadratic_(C, B * x, (A * x * x) - 1); // last pixel position at which a draw was performed let pX; let pY1; let pY2; while (conditionFn(x, y[0])) { pX = Math.floor(x); pY1 = Math.floor(y[0]); pY2 = Math.floor(y[1]); if (isFilled) { context.fillRect(centerX - pX - 1, centerY + pY2, 1, pY1 - pY2 + 1); context.fillRect(centerX + pX, centerY - pY1 - 1, 1, pY1 - pY2 + 1); } else { drawFn(centerX - pX - 1, centerY + pY1); drawFn(centerX + pX, centerY - pY1 - 1); } x++; y = solveQuadratic_(C, B * x, (A * x * x) - 1); } return pX || pY1 ? {x: pX, y: pY1} : null; }; // Last point drawn let lastPoint; if (verticalStepsFirst) { let forwardLeaning = false; if (slope1 > 0) forwardLeaning = true; // step vertically lastPoint = drawEllipseStepVertical_( forwardLeaning ? -radiusY : radiusY, (x, y) => { if (x === 0 && y > 0) return true; if (x === 0 && y < 0) return false; return y / x > slope1; } ); // step horizontally while slope is flat lastPoint = drawEllipseStepHorizontal_( lastPoint ? -lastPoint.x + .5 : .5, (x, y) => y / x > slope2 ) || {x: -lastPoint.x - .5, y: -lastPoint.y - .5}; // step vertically until back to start drawEllipseStepVertical_( lastPoint.y - .5, (x, y) => { if (forwardLeaning) return y > -radiusY; return y > radiusY; } ); } else { // step horizontally forward lastPoint = drawEllipseStepHorizontal_( .5, (x, y) => y / x > slope2 ); // step vertically while slope is steep lastPoint = drawEllipseStepVertical_( lastPoint ? lastPoint.y - .5 : radiusY, (x, y) => { if (x === 0 && y > 0) return true; if (x === 0 && y < 0) return false; return y / x > slope1; } ) || lastPoint; // step horizontally until back to start drawEllipseStepHorizontal_( -lastPoint.x + .5, x => x < 0 ); } return true; }; /** * @param {!number} size The diameter of the brush * @param {!string} color The css color of the brush * @param {?boolean} isEraser True if we want the brush mark for the eraser * @return {HTMLCanvasElement} a canvas with the brush mark printed on it */ const getBrushMark = function (size, color, isEraser) { size = ~~size; const canvas = document.createElement('canvas'); const roundedUpRadius = Math.ceil(size / 2); canvas.width = roundedUpRadius * 2; canvas.height = roundedUpRadius * 2; const {context, unmask} = createMaskingCanvas(canvas.getContext('2d', { willReadFrequently: true }), isEraser ? 'white' : color); context.imageSmoothingEnabled = false; // Small squares for pixel artists if (size <= 5) { let offset = 0; if (size % 2) offset = 1; if (isEraser) { context.fillStyle = getGuideColor(); context.fillRect(offset, offset, size, size); context.fillStyle = 'white'; context.fillRect(offset + 1, offset + 1, size - 2, size - 2); } else { context.fillRect(offset, offset, size, size); } } else { drawShearedEllipse_({ centerX: size / 2, centerY: size / 2, radiusX: size / 2, radiusY: size / 2, shearSlope: 0, isFilled: true }, context); if (isEraser) { // Add outline context.fillStyle = getGuideColor(); drawShearedEllipse_({ centerX: size / 2, centerY: size / 2, radiusX: size / 2, radiusY: size / 2, shearSlope: 0, isFilled: false, drawFn: (x, y) => context.fillRect(x, y, 1, 1) }, context); } } unmask(); return canvas; }; /** * Draw an ellipse, given the original axis-aligned radii and * an affine transformation. Returns false if the ellipse could * not be drawn; for instance, the matrix is non-invertible. * * @param {!options} options Parameters for the ellipse * @param {!paper.Point} options.position Center of ellipse * @param {!number} options.radiusX x-aligned radius of ellipse * @param {!number} options.radiusY y-aligned radius of ellipse * @param {!paper.Matrix} options.matrix affine transformation matrix * @param {?boolean} options.isFilled true if isFilled * @param {?number} options.thickness Thickness of outline, used only if isFilled is false. * @param {!CanvasRenderingContext2D} context for drawing * @return {boolean} true if anything was drawn, false if not */ const drawEllipse = function (options, context) { const positionX = options.position.x; const positionY = options.position.y; const radiusX = options.radiusX; const radiusY = options.radiusY; const matrix = options.matrix; const isFilled = options.isFilled; const thickness = options.thickness; let drawFn = null; if (!matrix.isInvertible()) return false; const inverse = matrix.clone().invert(); const needsMask = doesColorRequireMask(context.fillStyle); // If drawing a gradient, we need to draw the shape onto a temporary canvas, then draw the gradient atop that canvas // only where the shape appears. drawShearedEllipse draws some pixels twice, which would be a problem if the // gradient fades to transparent as those pixels would end up looking more opaque. Instead, mask in the gradient. // https://github.com/LLK/scratch-paint/issues/1152 // Outlines are drawn as a series of brush mark images and as such can't be drawn as gradients in the first place. let origContext; let tmpCanvas; const {width: canvasWidth, height: canvasHeight} = context.canvas; if (needsMask) { tmpCanvas = createCanvas(canvasWidth, canvasHeight); origContext = context; context = tmpCanvas.getContext('2d'); } if (!isFilled) { const brushMark = getBrushMark(thickness, needsMask ? 'black' : context.fillStyle); const roundedUpRadius = Math.ceil(thickness / 2); drawFn = (x, y) => { context.drawImage(brushMark, ~~x - roundedUpRadius, ~~y - roundedUpRadius); }; } // Calculate the ellipse formula // A, B, and C represent Ax^2 + Bxy + Cy^2 = 1 coefficients in a transformed ellipse formula const A = (inverse.a * inverse.a / radiusX / radiusX) + (inverse.b * inverse.b / radiusY / radiusY); const B = (2 * inverse.a * inverse.c / radiusX / radiusX) + (2 * inverse.b * inverse.d / radiusY / radiusY); const C = (inverse.c * inverse.c / radiusX / radiusX) + (inverse.d * inverse.d / radiusY / radiusY); // Convert to a sheared ellipse formula. All ellipses are equivalent to some sheared axis-aligned ellipse. // radiusA, radiusB, and slope are parameters of a skewed ellipse with the above formula const radiusB = 1 / Math.sqrt(C); const radiusA = Math.sqrt(-4 * C / ((B * B) - (4 * A * C))); const slope = B / 2 / C; const wasDrawn = drawShearedEllipse_({ centerX: positionX, centerY: positionY, radiusX: radiusA, radiusY: radiusB, shearSlope: slope, isFilled: isFilled, drawFn: drawFn }, context); // Mask in the gradient only where the shape was drawn, and draw it. Then draw the gradientified shape onto the // original canvas normally. if (needsMask && wasDrawn) { context.globalCompositeOperation = 'source-in'; context.fillStyle = origContext.fillStyle; context.fillRect(0, 0, canvasWidth, canvasHeight); origContext.drawImage(tmpCanvas, 0, 0); } return wasDrawn; }; const rowBlank_ = function (imageData, width, y) { for (let x = 0; x < width; ++x) { if (imageData.data[(y * width << 2) + (x << 2) + 3] !== 0) return false; } return true; }; const columnBlank_ = function (imageData, width, x, top, bottom) { for (let y = top; y < bottom; ++y) { if (imageData.data[(y * width << 2) + (x << 2) + 3] !== 0) return false; } return true; }; /** * Get bounds around the contents of a raster, trimming transparent pixels from edges. * Adapted from Tim Down's https://gist.github.com/timdown/021d9c8f2aabc7092df564996f5afbbf * @param {paper.Raster} raster The raster to get the bounds around * @param {paper.Rectangle} [rect] Optionally, an alternative bounding rectangle to limit the check to. * @returns {paper.Rectangle} The bounds around the opaque area of the passed raster * (or opaque within the passed rectangle) */ const getHitBounds = function (raster, rect) { const bounds = rect || raster.bounds; const width = bounds.width; const imageData = raster.getImageData(bounds); let top = 0; let bottom = imageData.height; let left = 0; let right = imageData.width; while (top < bottom && rowBlank_(imageData, width, top)) ++top; while (bottom - 1 > top && rowBlank_(imageData, width, bottom - 1)) --bottom; while (left < right && columnBlank_(imageData, width, left, top, bottom)) ++left; while (right - 1 > left && columnBlank_(imageData, width, right - 1, top, bottom)) --right; // Center an empty bitmap if (top === bottom) { top = bottom = imageData.height / 2; } if (left === right) { left = right = imageData.width / 2; } return new paper.Rectangle(left + bounds.left, top + bounds.top, right - left, bottom - top); }; const trim_ = function (raster) { const hitBounds = getHitBounds(raster); if (hitBounds.width && hitBounds.height) { return raster.getSubRaster(getHitBounds(raster)); } return null; }; /** * @param {boolean} shouldInsert True if the trimmed raster should be added to the active layer. * @returns {paper.Raster} raster layer with whitespace trimmed from ends, or null if there is * nothing on the raster layer. */ const getTrimmedRaster = function (shouldInsert) { const trimmedRaster = trim_(getRaster()); if (!trimmedRaster) return null; if (shouldInsert) { paper.project.activeLayer.addChild(trimmedRaster); } else { trimmedRaster.remove(); } return trimmedRaster; }; /** * @param {uint8array} uint8 uint8array * @returns {string} base64 data */ const uint8ToBase64 = function(uint8) { let binary = ''; const chunkSize = 0x8000; for (let i = 0; i < uint8.length; i += chunkSize) { const chunk = uint8.subarray(i, i + chunkSize); binary += String.fromCharCode.apply(null, chunk); } return btoa(binary); } /** * @returns {string} css for directions to custom fonts, used by