diff --git a/packages/graphics/src/const.js b/packages/graphics/src/const.js index 34679d6..e6df592 100644 --- a/packages/graphics/src/const.js +++ b/packages/graphics/src/const.js @@ -14,13 +14,13 @@ * @property {number} maxSegments=2048 - maximal number of segments in the curve (if adaptive = false, ignored) */ export const GRAPHICS_CURVES = { - adaptive: false, + adaptive: true, maxLength: 10, minSegments: 8, maxSegments: 2048, _segmentsCount(length, defaultSegments = 20) { - if (this.adaptive) + if (!this.adaptive) { return defaultSegments; } diff --git a/packages/graphics/src/const.js b/packages/graphics/src/const.js index 34679d6..e6df592 100644 --- a/packages/graphics/src/const.js +++ b/packages/graphics/src/const.js @@ -14,13 +14,13 @@ * @property {number} maxSegments=2048 - maximal number of segments in the curve (if adaptive = false, ignored) */ export const GRAPHICS_CURVES = { - adaptive: false, + adaptive: true, maxLength: 10, minSegments: 8, maxSegments: 2048, _segmentsCount(length, defaultSegments = 20) { - if (this.adaptive) + if (!this.adaptive) { return defaultSegments; } diff --git a/packages/graphics/src/utils/QuadraticUtils.js b/packages/graphics/src/utils/QuadraticUtils.js index 96229d4..c15511c 100644 --- a/packages/graphics/src/utils/QuadraticUtils.js +++ b/packages/graphics/src/utils/QuadraticUtils.js @@ -23,10 +23,10 @@ */ static curveLength(fromX, fromY, cpX, cpY, toX, toY) { - const ax = fromX - ((2.0 * cpX) + toX); - const ay = fromY - ((2.0 * cpY) + toY); - const bx = 2.0 * ((cpX - 2.0) * fromX); - const by = 2.0 * ((cpY - 2.0) * fromY); + const ax = fromX - (2.0 * cpX) + toX; + const ay = fromY - (2.0 * cpY) + toY; + const bx = (2.0 * cpX) - (2.0 * fromX); + const by = (2.0 * cpY) - (2.0 * fromY); const a = 4.0 * ((ax * ax) + (ay * ay)); const b = 4.0 * ((ax * bx) + (ay * by)); const c = (bx * bx) + (by * by); diff --git a/packages/graphics/src/const.js b/packages/graphics/src/const.js index 34679d6..e6df592 100644 --- a/packages/graphics/src/const.js +++ b/packages/graphics/src/const.js @@ -14,13 +14,13 @@ * @property {number} maxSegments=2048 - maximal number of segments in the curve (if adaptive = false, ignored) */ export const GRAPHICS_CURVES = { - adaptive: false, + adaptive: true, maxLength: 10, minSegments: 8, maxSegments: 2048, _segmentsCount(length, defaultSegments = 20) { - if (this.adaptive) + if (!this.adaptive) { return defaultSegments; } diff --git a/packages/graphics/src/utils/QuadraticUtils.js b/packages/graphics/src/utils/QuadraticUtils.js index 96229d4..c15511c 100644 --- a/packages/graphics/src/utils/QuadraticUtils.js +++ b/packages/graphics/src/utils/QuadraticUtils.js @@ -23,10 +23,10 @@ */ static curveLength(fromX, fromY, cpX, cpY, toX, toY) { - const ax = fromX - ((2.0 * cpX) + toX); - const ay = fromY - ((2.0 * cpY) + toY); - const bx = 2.0 * ((cpX - 2.0) * fromX); - const by = 2.0 * ((cpY - 2.0) * fromY); + const ax = fromX - (2.0 * cpX) + toX; + const ay = fromY - (2.0 * cpY) + toY; + const bx = (2.0 * cpX) - (2.0 * fromX); + const by = (2.0 * cpY) - (2.0 * fromY); const a = 4.0 * ((ax * ax) + (ay * ay)); const b = 4.0 * ((ax * bx) + (ay * by)); const c = (bx * bx) + (by * by); diff --git a/packages/graphics/test/index.js b/packages/graphics/test/index.js index 56e3802..dfad53d 100644 --- a/packages/graphics/test/index.js +++ b/packages/graphics/test/index.js @@ -1,6 +1,6 @@ // const MockPointer = require('../interaction/MockPointer'); const { Renderer, BatchRenderer } = require('@pixi/core'); -const { Graphics } = require('../'); +const { Graphics, GRAPHICS_CURVES } = require('../'); const { BLEND_MODES } = require('@pixi/constants'); const { Point } = require('@pixi/math'); const { skipHello } = require('@pixi/utils'); @@ -367,4 +367,28 @@ expect(data[1].shape.points).to.eql([250, 50, 100, 100, 50, 50]); }); }); + + describe('should support adaptive curves', function () + { + const defMode = GRAPHICS_CURVES.adaptive; + const defMaxLen = GRAPHICS_CURVES.maxLength; + const myMaxLen = GRAPHICS_CURVES.maxLength = 1.0; + const graphics = new Graphics(); + + GRAPHICS_CURVES.adaptive = true; + + graphics.beginFill(0xffffff, 1.0); + graphics.moveTo(610, 500); + graphics.quadraticCurveTo(600, 510, 590, 500); + graphics.endFill(); + + const pointsLen = graphics.geometry.graphicsData[0].shape.points.length / 2; + const arcLen = Math.PI / 2 * Math.sqrt(200); + const estimate = Math.ceil(arcLen / myMaxLen) + 1; + + expect(pointsLen).to.be.closeTo(estimate, 2.0); + + GRAPHICS_CURVES.adaptive = defMode; + GRAPHICS_CURVES.maxLength = defMaxLen; + }); });