Redo fill logic

This commit is contained in:
DD 2018-06-25 16:49:28 -04:00
parent c08c534bff
commit f4df0e07af
2 changed files with 22 additions and 20 deletions

View file

@ -1,6 +1,6 @@
import paper from '@scratch/paper'; import paper from '@scratch/paper';
import Modes from '../../lib/modes'; import Modes from '../../lib/modes';
import {drawRect} from '../bitmap'; import {fillRect} from '../bitmap';
import {createCanvas, getRaster} from '../layer'; import {createCanvas, getRaster} from '../layer';
import {clearSelection} from '../selection'; import {clearSelection} from '../selection';
import BoundingBoxTool from '../selection-tools/bounding-box-tool'; import BoundingBoxTool from '../selection-tools/bounding-box-tool';
@ -134,7 +134,7 @@ class RectTool extends paper.Tool {
const tmpCanvas = createCanvas(); const tmpCanvas = createCanvas();
const context = tmpCanvas.getContext('2d'); const context = tmpCanvas.getContext('2d');
context.fillStyle = this.color; context.fillStyle = this.color;
drawRect(this.rect, context); fillRect(this.rect, context);
getRaster().drawImage(tmpCanvas, new paper.Point()); getRaster().drawImage(tmpCanvas, new paper.Point());
this.rect.remove(); this.rect.remove();

View file

@ -523,7 +523,7 @@ const floodFillAll = function (x, y, color, context) {
* @param {!paper.Shape.Rectangle} rect The rectangle to draw to the canvas * @param {!paper.Shape.Rectangle} rect The rectangle to draw to the canvas
* @param {!HTMLCanvas2DContext} context The context in which to draw * @param {!HTMLCanvas2DContext} context The context in which to draw
*/ */
const drawRect = function (rect, context) { const fillRect = function (rect, context) {
// No rotation component to matrix // No rotation component to matrix
if (rect.matrix.b === 0 && rect.matrix.c === 0) { if (rect.matrix.b === 0 && rect.matrix.c === 0) {
const width = rect.size.width * rect.matrix.a; const width = rect.size.width * rect.matrix.a;
@ -541,25 +541,27 @@ const drawRect = function (rect, context) {
const heightPoint = 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 endPoint = rect.matrix.transform(new paper.Point(rect.size.width / 2, rect.size.height / 2));
const center = rect.matrix.transform(new paper.Point()); const center = rect.matrix.transform(new paper.Point());
forEachLinePoint(startPoint, widthPoint, (x, y) => { const points = [startPoint, widthPoint, heightPoint, endPoint].sort((a, b) => a.x - b.x);
context.fillRect(x, y, 1, 1);
}); const solveY = (point1, point2, x) => {
forEachLinePoint(startPoint, heightPoint, (x, y) => { if (point2.x === point1.x) return center.x > point1.x ? Number.NEGATIVE_INFINITY : Number.POSITIVE_INFINITY;
context.fillRect(x, y, 1, 1); return ((point2.y - point1.y) / (point2.x - point1.x) * (x - point1.x)) + point1.y;
}); };
forEachLinePoint(endPoint, widthPoint, (x, y) => { for (let x = Math.round(points[0].x); x < Math.round(points[3].x); x++) {
context.fillRect(x, y, 1, 1); const ys = [
}); solveY(startPoint, widthPoint, x + .5),
forEachLinePoint(endPoint, heightPoint, (x, y) => { solveY(startPoint, heightPoint, x + .5),
context.fillRect(x, y, 1, 1); solveY(endPoint, widthPoint, x + .5),
}); solveY(endPoint, heightPoint, x + .5)
floodFill(~~center.x, ~~center.y, context.fillStyle, context); ].sort((a, b) => a - b);
context.fillRect(x, Math.round(ys[1]), 1, Math.max(1, Math.round(ys[2]) - Math.round(ys[1])));
}
}; };
export { export {
convertToBitmap, convertToBitmap,
convertToVector, convertToVector,
drawRect, fillRect,
floodFill, floodFill,
floodFillAll, floodFillAll,
getBrushMark, getBrushMark,