| | 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'; |
| |
|
| | |
| | |
| | |
| | |
| | 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); |
| | } |
| | }; |
| | } |
| | |
| | originalContext.fillStyle = fillStyle; |
| | return { |
| | context: originalContext, |
| | unmask: () => {} |
| | }; |
| | }; |
| |
|
| | const forEachLinePoint = function (point1, point2, callback) { |
| | |
| | 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); |
| | } |
| | }; |
| |
|
| | |
| | |
| | |
| | |
| | |
| | |
| | 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]; |
| | }; |
| |
|
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | 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; |
| | } |
| | |
| | const A = (1 / radiusX / radiusX) + (shearSlope * shearSlope / radiusY / radiusY); |
| | const B = -2 * shearSlope / radiusY / radiusY; |
| | const C = 1 / radiusY / radiusY; |
| | |
| | const slope1 = ((-2 * A) - B) / ((2 * C) + B); |
| | |
| | const slope2 = (-(2 * A) + B) / (-(2 * C) + B); |
| | const verticalStepsFirst = slope1 > slope2; |
| |
|
| | |
| | |
| | |
| | |
| | |
| | |
| | const drawEllipseStepVertical_ = function (startY, conditionFn) { |
| | |
| | let y = startY; |
| | let x = solveQuadratic_(A, B * y, (C * y * y) - 1); |
| | |
| | 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; |
| | }; |
| |
|
| | |
| | |
| | |
| | |
| | |
| | |
| | const drawEllipseStepHorizontal_ = function (startX, conditionFn) { |
| | |
| | let x = startX; |
| | let y = solveQuadratic_(C, B * x, (A * x * x) - 1); |
| | |
| | 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; |
| | }; |
| |
|
| | |
| | let lastPoint; |
| | if (verticalStepsFirst) { |
| | let forwardLeaning = false; |
| | if (slope1 > 0) forwardLeaning = true; |
| |
|
| | |
| | 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; |
| | } |
| | ); |
| | |
| | lastPoint = drawEllipseStepHorizontal_( |
| | lastPoint ? -lastPoint.x + .5 : .5, |
| | (x, y) => y / x > slope2 |
| | ) || {x: -lastPoint.x - .5, y: -lastPoint.y - .5}; |
| | |
| | drawEllipseStepVertical_( |
| | lastPoint.y - .5, |
| | (x, y) => { |
| | if (forwardLeaning) return y > -radiusY; |
| | return y > radiusY; |
| | } |
| | ); |
| | } else { |
| | |
| | lastPoint = drawEllipseStepHorizontal_( |
| | .5, |
| | (x, y) => y / x > slope2 |
| | ); |
| | |
| | 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; |
| | |
| | drawEllipseStepHorizontal_( |
| | -lastPoint.x + .5, |
| | x => x < 0 |
| | ); |
| | } |
| | return true; |
| | }; |
| |
|
| | |
| | |
| | |
| | |
| | |
| | |
| | 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; |
| | |
| | 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) { |
| | |
| | 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; |
| | }; |
| |
|
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | 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); |
| |
|
| | |
| | |
| | |
| | |
| | |
| | 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); |
| | }; |
| | } |
| |
|
| | |
| | |
| | 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); |
| |
|
| | |
| | |
| | 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); |
| |
|
| | |
| | |
| | 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; |
| | }; |
| |
|
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | 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; |
| |
|
| | |
| | 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; |
| | }; |
| |
|
| | |
| | |
| | |
| | |
| | |
| | const getTrimmedRaster = function (shouldInsert) { |
| | const trimmedRaster = trim_(getRaster()); |
| | if (!trimmedRaster) return null; |
| | if (shouldInsert) { |
| | paper.project.activeLayer.addChild(trimmedRaster); |
| | } else { |
| | trimmedRaster.remove(); |
| | } |
| | return trimmedRaster; |
| | }; |
| |
|
| | |
| | |
| | |
| | |
| | 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); |
| | } |
| |
|
| | |
| | |
| | |
| | const generateCustomFontsCSS = function() { |
| | if (!ReduxStore) return ''; |
| | const fonts = ReduxStore.getState().scratchPaint.customFonts.filter(f => !f.system); |
| |
|
| | let fontCSS = ''; |
| | for (const font of fonts) { |
| | const base64 = uint8ToBase64(font.data); |
| |
|
| | |
| | let format = font.format.toLowerCase(); |
| | if (format === 'otf') format = 'opentype'; |
| | if (format === 'ttf') format = 'truetype'; |
| |
|
| | fontCSS += "@font-face {\n"; |
| | fontCSS += `font-family: "${font.name}";\n`; |
| | fontCSS += `src: url('data:font/${format};base64,${base64}') format('${format}');\n`; |
| | fontCSS += `font-display: block;\n`; |
| | fontCSS += "}\n"; |
| | } |
| |
|
| | return fontCSS; |
| | }; |
| |
|
| | const convertToBitmap = function (clearSelectedItems, onUpdateImage, optFontInlineFn) { |
| | |
| | |
| |
|
| | clearSelection(clearSelectedItems); |
| |
|
| | |
| | const guideLayers = hideGuideLayers(true ); |
| | const bounds = paper.project.activeLayer.drawnBounds; |
| | const svg = paper.project.exportSVG({ |
| | bounds: 'content', |
| | matrix: new paper.Matrix().translate(-bounds.x, -bounds.y) |
| | }); |
| | showGuideLayers(guideLayers); |
| |
|
| | svg.setAttribute('shape-rendering', 'crispEdges'); |
| |
|
| | |
| | |
| | |
| | svg.setAttribute('text-rendering', 'geometricPrecision'); |
| |
|
| | const customFontCSS = generateCustomFontsCSS(); |
| | if (customFontCSS) { |
| | let defs = svg.querySelector('defs'); |
| | if (!defs) { |
| | defs = document.createElementNS('http://www.w3.org/2000/svg', 'defs'); |
| | svg.insertBefore(defs, svg.firstChild); |
| | } |
| |
|
| | const style = document.createElementNS('http://www.w3.org/2000/svg', 'style'); |
| | style.setAttribute('type', 'text/css'); |
| | style.innerHTML = customFontCSS; |
| | defs.appendChild(style); |
| | } |
| |
|
| | let svgString = (new XMLSerializer()).serializeToString(svg); |
| | if (optFontInlineFn) { |
| | svgString = optFontInlineFn(svgString); |
| | } else { |
| | log.error('Fonts may be converted to bitmap incorrectly if fontInlineFn prop is not set on PaintEditor.'); |
| | } |
| |
|
| | |
| | const img = new Image(); |
| | img.onload = () => { |
| | if (img.width && img.height) { |
| | getRaster().drawImage( |
| | img, |
| | new paper.Point(Math.floor(bounds.topLeft.x), Math.floor(bounds.topLeft.y))); |
| | } |
| | for (let i = paper.project.activeLayer.children.length - 1; i >= 0; i--) { |
| | const item = paper.project.activeLayer.children[i]; |
| | if (item.clipMask === false) { |
| | item.remove(); |
| | } else { |
| | |
| | item.size.height = ART_BOARD_HEIGHT; |
| | item.size.width = ART_BOARD_WIDTH; |
| | item.setPosition(CENTER); |
| | } |
| | } |
| | onUpdateImage(false , Formats.BITMAP ); |
| | }; |
| | img.onerror = () => { |
| | |
| | |
| | const raster = paper.project.activeLayer.rasterize(72, false ); |
| | raster.onLoad = () => { |
| | if (raster.canvas.width && raster.canvas.height) { |
| | getRaster().drawImage(raster.canvas, raster.bounds.topLeft); |
| | } |
| | paper.project.activeLayer.removeChildren(); |
| | onUpdateImage(false , Formats.BITMAP ); |
| | }; |
| | }; |
| | |
| | img.src = `data:image/svg+xml;utf8,${encodeURIComponent(svgString)}`; |
| | }; |
| |
|
| | const convertToVector = function (clearSelectedItems, onUpdateImage) { |
| | clearSelection(clearSelectedItems); |
| | for (const item of paper.project.activeLayer.children) { |
| | if (item.clipMask === true) { |
| | |
| | item.size.height = MAX_WORKSPACE_BOUNDS.height; |
| | item.size.width = MAX_WORKSPACE_BOUNDS.width; |
| | item.setPosition(CENTER); |
| | } |
| | } |
| | getTrimmedRaster(true ); |
| |
|
| | clearRaster(); |
| | onUpdateImage(false , Formats.VECTOR ); |
| | }; |
| |
|
| | const getColor_ = function (x, y, context) { |
| | return context.getImageData(x, y, 1, 1).data; |
| | }; |
| |
|
| | const matchesColor_ = function (x, y, imageData, oldColor) { |
| | const index = ((y * imageData.width) + x) * 4; |
| | return ( |
| | imageData.data[index + 0] === oldColor[0] && |
| | imageData.data[index + 1] === oldColor[1] && |
| | imageData.data[index + 2] === oldColor[2] && |
| | imageData.data[index + 3] === oldColor[3] |
| | ); |
| | }; |
| |
|
| | const colorPixel_ = function (x, y, imageData, newColor) { |
| | const index = ((y * imageData.width) + x) * 4; |
| | imageData.data[index + 0] = newColor[0]; |
| | imageData.data[index + 1] = newColor[1]; |
| | imageData.data[index + 2] = newColor[2]; |
| | imageData.data[index + 3] = newColor[3]; |
| | }; |
| |
|
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | const floodFillInternal_ = function (x, y, sourceImageData, destImageData, newColor, oldColor, stack) { |
| | while (y > 0 && matchesColor_(x, y - 1, sourceImageData, oldColor)) { |
| | y--; |
| | } |
| | let lastLeftMatchedColor = false; |
| | let lastRightMatchedColor = false; |
| | for (; y < sourceImageData.height; y++) { |
| | if (!matchesColor_(x, y, sourceImageData, oldColor)) break; |
| | colorPixel_(x, y, sourceImageData, newColor); |
| | colorPixel_(x, y, destImageData, newColor); |
| | if (x > 0) { |
| | if (matchesColor_(x - 1, y, sourceImageData, oldColor)) { |
| | if (!lastLeftMatchedColor) { |
| | stack.push([x - 1, y]); |
| | lastLeftMatchedColor = true; |
| | } |
| | } else { |
| | lastLeftMatchedColor = false; |
| | } |
| | } |
| | if (x < sourceImageData.width - 1) { |
| | if (matchesColor_(x + 1, y, sourceImageData, oldColor)) { |
| | if (!lastRightMatchedColor) { |
| | stack.push([x + 1, y]); |
| | lastRightMatchedColor = true; |
| | } |
| | } else { |
| | lastRightMatchedColor = false; |
| | } |
| | } |
| | } |
| | }; |
| |
|
| | |
| | |
| | |
| | |
| | |
| | const fillStyleToColor_ = function (fillStyleString) { |
| | const tmpCanvas = document.createElement('canvas'); |
| | tmpCanvas.width = 1; |
| | tmpCanvas.height = 1; |
| | const context = tmpCanvas.getContext('2d', { willReadFrequently: true }); |
| | context.fillStyle = fillStyleString; |
| | context.fillRect(0, 0, 1, 1); |
| | return context.getImageData(0, 0, 1, 1).data; |
| | }; |
| |
|
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | const floodFill = function (x, y, color, sourceContext, destContext) { |
| | x = ~~x; |
| | y = ~~y; |
| | const newColor = fillStyleToColor_(color); |
| | const oldColor = getColor_(x, y, sourceContext); |
| | const sourceImageData = sourceContext.getImageData(0, 0, sourceContext.canvas.width, sourceContext.canvas.height); |
| | let destImageData = sourceImageData; |
| | if (destContext !== sourceContext) { |
| | destImageData = new ImageData(sourceContext.canvas.width, sourceContext.canvas.height); |
| | } |
| | if (oldColor[0] === newColor[0] && |
| | oldColor[1] === newColor[1] && |
| | oldColor[2] === newColor[2] && |
| | oldColor[3] === newColor[3]) { |
| | return false; |
| | } |
| | const stack = [[x, y]]; |
| | while (stack.length) { |
| | const pop = stack.pop(); |
| | floodFillInternal_(pop[0], pop[1], sourceImageData, destImageData, newColor, oldColor, stack); |
| | } |
| | destContext.putImageData(destImageData, 0, 0); |
| | return true; |
| | }; |
| |
|
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | const floodFillAll = function (x, y, color, sourceContext, destContext) { |
| | x = ~~x; |
| | y = ~~y; |
| | const newColor = fillStyleToColor_(color); |
| | const oldColor = getColor_(x, y, sourceContext); |
| | const sourceImageData = sourceContext.getImageData(0, 0, sourceContext.canvas.width, sourceContext.canvas.height); |
| | let destImageData = sourceImageData; |
| | if (destContext !== sourceContext) { |
| | destImageData = new ImageData(sourceContext.canvas.width, sourceContext.canvas.height); |
| | } |
| | if (oldColor[0] === newColor[0] && |
| | oldColor[1] === newColor[1] && |
| | oldColor[2] === newColor[2] && |
| | oldColor[3] === newColor[3]) { |
| | return false; |
| | } |
| | for (let i = 0; i < sourceImageData.width; i++) { |
| | for (let j = 0; j < sourceImageData.height; j++) { |
| | if (matchesColor_(i, j, sourceImageData, oldColor)) { |
| | colorPixel_(i, j, destImageData, newColor); |
| | } |
| | } |
| | } |
| | destContext.putImageData(destImageData, 0, 0); |
| | return true; |
| | }; |
| |
|
| | |
| | |
| | |
| | |
| | const fillRect = function (rect, context) { |
| | |
| | if (rect.matrix.b === 0 && rect.matrix.c === 0) { |
| | const width = rect.size.width * rect.matrix.a; |
| | const height = rect.size.height * rect.matrix.d; |
| | context.fillRect( |
| | Math.round(rect.matrix.tx - (width / 2)), |
| | Math.round(rect.matrix.ty - (height / 2)), |
| | Math.round(width), |
| | Math.round(height) |
| | ); |
| | return; |
| | } |
| | const startPoint = rect.matrix.transform(new paper.Point(-rect.size.width / 2, -rect.size.height / 2)); |
| | const widthPoint = rect.matrix.transform(new paper.Point(rect.size.width / 2, -rect.size.height / 2)); |
| | const heightPoint = rect.matrix.transform(new paper.Point(-rect.size.width / 2, rect.size.height / 2)); |
| | const endPoint = rect.matrix.transform(new paper.Point(rect.size.width / 2, rect.size.height / 2)); |
| | const center = rect.matrix.transform(new paper.Point()); |
| | const points = [startPoint, widthPoint, heightPoint, endPoint].sort((a, b) => a.x - b.x); |
| |
|
| | const solveY = (point1, point2, x) => { |
| | if (point2.x === point1.x) return center.x > point1.x ? Number.NEGATIVE_INFINITY : Number.POSITIVE_INFINITY; |
| | return ((point2.y - point1.y) / (point2.x - point1.x) * (x - point1.x)) + point1.y; |
| | }; |
| | for (let x = Math.round(points[0].x); x < Math.round(points[3].x); x++) { |
| | const ys = [ |
| | solveY(startPoint, widthPoint, x + .5), |
| | solveY(startPoint, heightPoint, x + .5), |
| | solveY(endPoint, widthPoint, x + .5), |
| | solveY(endPoint, heightPoint, x + .5) |
| | ].sort((a, b) => a - b); |
| | context.fillRect(x, Math.round(ys[1]), 1, Math.max(1, Math.round(ys[2]) - Math.round(ys[1]))); |
| | } |
| | }; |
| |
|
| | |
| | |
| | |
| | |
| | |
| | const outlineRect = function (rect, thickness, context) { |
| | const brushMark = getBrushMark(thickness, context.fillStyle); |
| | const roundedUpRadius = Math.ceil(thickness / 2); |
| | const drawFn = (x, y) => { |
| | context.drawImage(brushMark, ~~x - roundedUpRadius, ~~y - roundedUpRadius); |
| | }; |
| |
|
| | const needsMask = doesColorRequireMask(context.fillStyle); |
| |
|
| | |
| | |
| | |
| | let origContext; |
| | let tmpCanvas; |
| | const {width: canvasWidth, height: canvasHeight} = context.canvas; |
| | if (needsMask) { |
| | tmpCanvas = createCanvas(canvasWidth, canvasHeight); |
| | origContext = context; |
| | context = tmpCanvas.getContext('2d'); |
| | } |
| |
|
| | const startPoint = rect.matrix.transform(new paper.Point(-rect.size.width / 2, -rect.size.height / 2)); |
| | const widthPoint = rect.matrix.transform(new paper.Point(rect.size.width / 2, -rect.size.height / 2)); |
| | const heightPoint = rect.matrix.transform(new paper.Point(-rect.size.width / 2, rect.size.height / 2)); |
| | const endPoint = rect.matrix.transform(new paper.Point(rect.size.width / 2, rect.size.height / 2)); |
| |
|
| | forEachLinePoint(startPoint, widthPoint, drawFn); |
| | forEachLinePoint(startPoint, heightPoint, drawFn); |
| | forEachLinePoint(endPoint, widthPoint, drawFn); |
| | forEachLinePoint(endPoint, heightPoint, drawFn); |
| |
|
| | |
| | |
| | if (needsMask) { |
| | context.globalCompositeOperation = 'source-in'; |
| | context.fillStyle = origContext.fillStyle; |
| | context.fillRect(0, 0, canvasWidth, canvasHeight); |
| | origContext.drawImage(tmpCanvas, 0, 0); |
| | } |
| |
|
| | }; |
| |
|
| | const flipBitmapHorizontal = function (canvas) { |
| | const tmpCanvas = createCanvas(canvas.width, canvas.height); |
| | const context = tmpCanvas.getContext('2d'); |
| | context.save(); |
| | context.scale(-1, 1); |
| | context.drawImage(canvas, 0, 0, -tmpCanvas.width, tmpCanvas.height); |
| | context.restore(); |
| | return tmpCanvas; |
| | }; |
| |
|
| | const flipBitmapVertical = function (canvas) { |
| | const tmpCanvas = createCanvas(canvas.width, canvas.height); |
| | const context = tmpCanvas.getContext('2d'); |
| | context.save(); |
| | context.scale(1, -1); |
| | context.drawImage(canvas, 0, 0, tmpCanvas.width, -tmpCanvas.height); |
| | context.restore(); |
| | return tmpCanvas; |
| | }; |
| |
|
| | const scaleBitmap = function (canvas, scale) { |
| | let tmpCanvas = createCanvas(Math.round(canvas.width * Math.abs(scale.x)), canvas.height); |
| | if (scale.x < 0) { |
| | canvas = flipBitmapHorizontal(canvas); |
| | } |
| | tmpCanvas.getContext('2d').drawImage(canvas, 0, 0, tmpCanvas.width, tmpCanvas.height); |
| | canvas = tmpCanvas; |
| | tmpCanvas = createCanvas(canvas.width, Math.round(canvas.height * Math.abs(scale.y))); |
| | if (scale.y < 0) { |
| | canvas = flipBitmapVertical(canvas); |
| | } |
| | tmpCanvas.getContext('2d').drawImage(canvas, 0, 0, tmpCanvas.width, tmpCanvas.height); |
| | return tmpCanvas; |
| | }; |
| |
|
| | |
| | |
| | |
| | |
| | |
| | const maybeApplyScaleToCanvas_ = function (item) { |
| | |
| | |
| | |
| | |
| | const decomposed = item.matrix.decompose(); |
| | if (Math.abs(decomposed.scaling.x) < 1 && Math.abs(decomposed.scaling.y) < 1 && |
| | decomposed.scaling.x !== 0 && decomposed.scaling.y !== 0) { |
| | item.canvas = scaleBitmap(item.canvas, decomposed.scaling); |
| | if (item.data && item.data.expanded) { |
| | item.data.expanded.canvas = scaleBitmap(item.data.expanded.canvas, decomposed.scaling); |
| | } |
| | |
| | item.matrix.append( |
| | new paper.Matrix().scale(new paper.Point(1 / decomposed.scaling.x, 1 / decomposed.scaling.y))); |
| | } |
| | }; |
| |
|
| | |
| | |
| | |
| | |
| | |
| | |
| | const commitArbitraryTransformation_ = function (item, destination) { |
| | |
| | const tmpCanvas = createCanvas(); |
| | const context = tmpCanvas.getContext('2d'); |
| | |
| | const rect = new paper.Shape.Rectangle(new paper.Point(), item.size); |
| | rect.matrix = item.matrix; |
| | fillRect(rect, context); |
| | rect.remove(); |
| | context.globalCompositeOperation = 'source-in'; |
| |
|
| | |
| | const m = item.matrix; |
| | context.transform(m.a, m.b, m.c, m.d, m.tx, m.ty); |
| | let canvas = item.canvas; |
| | if (item.data && item.data.expanded) { |
| | canvas = item.data.expanded.canvas; |
| | } |
| | context.transform(1, 0, 0, 1, -canvas.width / 2, -canvas.height / 2); |
| | context.drawImage(canvas, 0, 0); |
| |
|
| | |
| | destination.drawImage(tmpCanvas, new paper.Point()); |
| | }; |
| |
|
| | |
| | |
| | |
| | |
| | |
| | |
| | const commitSelectionToBitmap = function (selection, bitmap) { |
| | if (!selection.matrix.isInvertible()) { |
| | return; |
| | } |
| |
|
| | maybeApplyScaleToCanvas_(selection); |
| | commitArbitraryTransformation_(selection, bitmap); |
| | }; |
| |
|
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | const _paperColorToCanvasStyle = function (color, context) { |
| | if (!color) return null; |
| | if (color.type === 'gradient') { |
| | let canvasGradient; |
| | const {origin, destination} = color; |
| | if (color.gradient.radial) { |
| | |
| | |
| | let {highlight} = color; |
| | const start = highlight || origin; |
| | const radius = destination.getDistance(origin); |
| | if (highlight) { |
| | const vector = highlight.subtract(origin); |
| | if (vector.getLength() > radius) { |
| | |
| | highlight = origin.add(vector.normalize(radius - 0.1)); |
| | } |
| | } |
| | canvasGradient = context.createRadialGradient( |
| | start.x, start.y, |
| | 0, |
| | origin.x, origin.y, |
| | radius |
| | ); |
| | } else { |
| | canvasGradient = context.createLinearGradient( |
| | origin.x, origin.y, |
| | destination.x, destination.y |
| | ); |
| | } |
| |
|
| | const {stops} = color.gradient; |
| | |
| | |
| | for (let i = 0, len = stops.length; i < len; i++) { |
| | const stop = stops[i]; |
| | const offset = stop.offset; |
| | canvasGradient.addColorStop( |
| | offset || i / (len - 1), |
| | stop.color.toCSS() |
| | ); |
| | } |
| | return canvasGradient; |
| | } |
| | return color.toCSS(); |
| | }; |
| |
|
| | |
| | |
| | |
| | |
| | |
| | const commitOvalToBitmap = function (oval, bitmap) { |
| | const radiusX = Math.abs(oval.size.width / 2); |
| | const radiusY = Math.abs(oval.size.height / 2); |
| | const context = bitmap.getContext('2d'); |
| | const filled = oval.strokeWidth === 0; |
| |
|
| | const canvasColor = _paperColorToCanvasStyle(filled ? oval.fillColor : oval.strokeColor, context); |
| | |
| | if (!canvasColor) return; |
| |
|
| | context.fillStyle = canvasColor; |
| |
|
| | const drew = drawEllipse({ |
| | position: oval.position, |
| | radiusX, |
| | radiusY, |
| | matrix: oval.matrix, |
| | isFilled: filled, |
| | thickness: oval.strokeWidth / paper.view.zoom |
| | }, context); |
| |
|
| | return drew; |
| | }; |
| |
|
| | |
| | |
| | |
| | |
| | const commitRectToBitmap = function (rect, bitmap) { |
| | const tmpCanvas = createCanvas(); |
| | const context = tmpCanvas.getContext('2d'); |
| | const filled = rect.strokeWidth === 0; |
| |
|
| | const canvasColor = _paperColorToCanvasStyle(filled ? rect.fillColor : rect.strokeColor, context); |
| | |
| | if (!canvasColor) return; |
| |
|
| | context.fillStyle = canvasColor; |
| |
|
| | if (filled) { |
| | fillRect(rect, context); |
| | } else { |
| | outlineRect(rect, rect.strokeWidth / paper.view.zoom, context); |
| | } |
| | bitmap.drawImage(tmpCanvas, new paper.Point()); |
| | }; |
| |
|
| | const selectAllBitmap = function (clearSelectedItems) { |
| | clearSelection(clearSelectedItems); |
| |
|
| | |
| | const trimmedRaster = getTrimmedRaster(true ); |
| | if (trimmedRaster) { |
| | trimmedRaster.selected = true; |
| | } |
| |
|
| | |
| | clearRaster(); |
| | }; |
| |
|
| | export { |
| | doesColorRequireMask, |
| | createMaskingCanvas, |
| | commitSelectionToBitmap, |
| | commitOvalToBitmap, |
| | commitRectToBitmap, |
| | convertToBitmap, |
| | convertToVector, |
| | fillRect, |
| | outlineRect, |
| | floodFill, |
| | floodFillAll, |
| | getBrushMark, |
| | getHitBounds, |
| | getTrimmedRaster, |
| | drawEllipse, |
| | forEachLinePoint, |
| | flipBitmapHorizontal, |
| | flipBitmapVertical, |
| | scaleBitmap, |
| | selectAllBitmap |
| | }; |
| |
|