From 14874297d0190861d3101e6097a8e9bdb5e49afc Mon Sep 17 00:00:00 2001 From: Andrea Bogazzi Date: Sun, 5 Aug 2018 09:55:53 +0200 Subject: [PATCH] Remove caching of some path parsing steps (#5140) * disable caching * missing jsdocs * missing jsdocs * added jsdocs --- HEADER.js | 22 ++++++++++++++++++++++ src/util/arc.js | 32 ++++++++++++++------------------ 2 files changed, 36 insertions(+), 18 deletions(-) diff --git a/HEADER.js b/HEADER.js index 606fee7d4ea..119611db7d6 100644 --- a/HEADER.js +++ b/HEADER.js @@ -140,6 +140,28 @@ fabric.devicePixelRatio = fabric.window.devicePixelRatio || */ fabric.browserShadowBlurConstant = 1; +/** + * This object contains the result of arc to beizer conversion for faster retrieving if the same arc needs to be converted again. + * It was an internal variable, is accessible since version 2.3.4 + */ +fabric.arcToSegmentsCache = { }; + +/** + * This object keeps the results of the boundsOfCurve calculation mapped by the joined arguments necessary to calculate it. + * It does speed up calculation, if you parse and add always the same paths, but in case of heavy usage of freedrawing + * you do not get any speed benefit and you get a big object in memory. + * The object was a private variable before, while now is appended to the lib so that you have access to it and you + * can eventually clear it. + * It was an internal variable, is accessible since version 2.3.4 + */ +fabric.boundsOfCurveCache = { }; + +/** + * If disabled boundsOfCurveCache is not used. For apps that make heavy usage of pencil drawing probably disabling it is better + * @default true + */ +fabric.cachesBoundsOfCurve = true; + fabric.initFilterBackend = function() { if (fabric.enableGLFiltering && fabric.isWebglSupported && fabric.isWebglSupported(fabric.textureSize)) { console.log('max texture size: ' + fabric.maxTextureSize); diff --git a/src/util/arc.js b/src/util/arc.js index c4b81d24015..48507a9fd70 100644 --- a/src/util/arc.js +++ b/src/util/arc.js @@ -1,9 +1,6 @@ (function() { - var arcToSegmentsCache = { }, - segmentToBezierCache = { }, - boundsOfCurveCache = { }, - _join = Array.prototype.join; + var _join = Array.prototype.join; /* Adapted from http://dxr.mozilla.org/mozilla-central/source/content/svg/content/src/nsSVGPathDataParser.cpp * by Andrea Bogazzi code is under MPL. if you don't have a copy of the license you can take it here @@ -11,8 +8,8 @@ */ function arcToSegments(toX, toY, rx, ry, large, sweep, rotateX) { var argsString = _join.call(arguments); - if (arcToSegmentsCache[argsString]) { - return arcToSegmentsCache[argsString]; + if (fabric.arcToSegmentsCache[argsString]) { + return fabric.arcToSegmentsCache[argsString]; } var PI = Math.PI, th = rotateX * PI / 180, @@ -66,16 +63,11 @@ mTheta = th3; th3 += mDelta; } - arcToSegmentsCache[argsString] = result; + fabric.arcToSegmentsCache[argsString] = result; return result; } function segmentToBezier(th2, th3, cosTh, sinTh, rx, ry, cx1, cy1, mT, fromX, fromY) { - var argsString2 = _join.call(arguments); - if (segmentToBezierCache[argsString2]) { - return segmentToBezierCache[argsString2]; - } - var costh2 = fabric.util.cos(th2), sinth2 = fabric.util.sin(th2), costh3 = fabric.util.cos(th3), @@ -87,12 +79,11 @@ cp2X = toX + mT * ( cosTh * rx * sinth3 + sinTh * ry * costh3), cp2Y = toY + mT * ( sinTh * rx * sinth3 - cosTh * ry * costh3); - segmentToBezierCache[argsString2] = [ + return [ cp1X, cp1Y, cp2X, cp2Y, toX, toY ]; - return segmentToBezierCache[argsString2]; } /* @@ -178,9 +169,12 @@ */ // taken from http://jsbin.com/ivomiq/56/edit no credits available for that. function getBoundsOfCurve(x0, y0, x1, y1, x2, y2, x3, y3) { - var argsString = _join.call(arguments); - if (boundsOfCurveCache[argsString]) { - return boundsOfCurveCache[argsString]; + var argsString; + if (fabric.cachesBoundsOfCurve) { + argsString = _join.call(arguments); + if (fabric.boundsOfCurveCache[argsString]) { + return fabric.boundsOfCurveCache[argsString]; + } } var sqrt = Math.sqrt, @@ -250,7 +244,9 @@ y: max.apply(null, bounds[1]) } ]; - boundsOfCurveCache[argsString] = result; + if (fabric.cachesBoundsOfCurve) { + fabric.boundsOfCurveCache[argsString] = result; + } return result; }