-
-
Notifications
You must be signed in to change notification settings - Fork 3.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix(): abort concurrent rendering #8218
Closed
Closed
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
8568ae0
init
ShaMan123 d467e3f
Update static_canvas.class.ts
ShaMan123 97f316c
Update static_canvas.class.ts
ShaMan123 8722733
`cancelRequestedRender` => `abortRendering`
ShaMan123 908aa38
Update static_canvas.class.ts
ShaMan123 279b1af
Merge branch 'master' into abort-concurrent-render
ShaMan123 bc2d9f0
imports
ShaMan123 312f2da
Update static_canvas.class.ts
ShaMan123 8c990f9
Update static_canvas.class.ts
ShaMan123 7799db7
Update static_canvas.class.ts
ShaMan123 8440031
Update static_canvas.class.ts
ShaMan123 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -38,9 +38,8 @@ import { removeFromArray } from './util/internals'; | |
* @param {Object} [options] Options object | ||
* @return {Object} thisArg | ||
*/ | ||
initialize: function(el, options) { | ||
options || (options = { }); | ||
this.renderAndResetBound = this.renderAndReset.bind(this); | ||
initialize: function (el, options) { | ||
options || (options = {}); | ||
this.requestRenderAllBound = this.requestRenderAll.bind(this); | ||
this._initStatic(el, options); | ||
}, | ||
|
@@ -171,7 +170,7 @@ import { removeFromArray } from './util/internals'; | |
* The coordinates get updated with @method calcViewportBoundaries. | ||
* @memberOf fabric.StaticCanvas.prototype | ||
*/ | ||
vptCoords: { }, | ||
vptCoords: {}, | ||
|
||
/** | ||
* Based on vptCoords and object.aCoords, skip rendering of objects that | ||
|
@@ -199,8 +198,10 @@ import { removeFromArray } from './util/internals'; | |
* @param {HTMLElement | String} el <canvas> element to initialize instance on | ||
* @param {Object} [options] Options object | ||
*/ | ||
_initStatic: function(el, options) { | ||
_initStatic: function (el, options) { | ||
this._objects = []; | ||
// holds refs to active abort controllers that control rendering | ||
this.__abortControllers = {}; | ||
this._createLowerCanvas(el); | ||
this._initOptions(options); | ||
// only initialize retina scaling once | ||
|
@@ -213,22 +214,22 @@ import { removeFromArray } from './util/internals'; | |
/** | ||
* @private | ||
*/ | ||
_isRetinaScaling: function() { | ||
_isRetinaScaling: function () { | ||
return (config.devicePixelRatio > 1 && this.enableRetinaScaling); | ||
}, | ||
|
||
/** | ||
* @private | ||
* @return {Number} retinaScaling if applied, otherwise 1; | ||
*/ | ||
getRetinaScaling: function() { | ||
getRetinaScaling: function () { | ||
return this._isRetinaScaling() ? Math.max(1, config.devicePixelRatio) : 1; | ||
}, | ||
|
||
/** | ||
* @private | ||
*/ | ||
_initRetinaScaling: function() { | ||
_initRetinaScaling: function () { | ||
if (!this._isRetinaScaling()) { | ||
return; | ||
} | ||
|
@@ -239,7 +240,7 @@ import { removeFromArray } from './util/internals'; | |
} | ||
}, | ||
|
||
__initRetinaScaling: function(scaleRatio, canvas, context) { | ||
__initRetinaScaling: function (scaleRatio, canvas, context) { | ||
canvas.setAttribute('width', this.width * scaleRatio); | ||
canvas.setAttribute('height', this.height * scaleRatio); | ||
context.scale(scaleRatio, scaleRatio); | ||
|
@@ -260,13 +261,13 @@ import { removeFromArray } from './util/internals'; | |
/** | ||
* @private | ||
*/ | ||
_createCanvasElement: function() { | ||
_createCanvasElement: function () { | ||
var element = createCanvasElement(); | ||
if (!element) { | ||
throw CANVAS_INIT_ERROR; | ||
} | ||
if (!element.style) { | ||
element.style = { }; | ||
element.style = {}; | ||
} | ||
if (typeof element.getContext === 'undefined') { | ||
throw CANVAS_INIT_ERROR; | ||
|
@@ -472,9 +473,9 @@ import { removeFromArray } from './util/internals'; | |
*/ | ||
setViewportTransform: function (vpt) { | ||
var activeObject = this._activeObject, | ||
backgroundObject = this.backgroundImage, | ||
overlayObject = this.overlayImage, | ||
object, i, len; | ||
backgroundObject = this.backgroundImage, | ||
overlayObject = this.overlayImage, | ||
object, i, len; | ||
this.viewportTransform = vpt; | ||
for (i = 0, len = this._objects.length; i < len; i++) { | ||
object = this._objects[i]; | ||
|
@@ -602,7 +603,7 @@ import { removeFromArray } from './util/internals'; | |
* @private | ||
* @param {fabric.Object} obj Object that was added | ||
*/ | ||
_onObjectAdded: function(obj) { | ||
_onObjectAdded: function (obj) { | ||
this.stateful && obj.setupState(); | ||
if (obj.canvas && obj.canvas !== this) { | ||
/* _DEV_MODE_START_ */ | ||
|
@@ -621,7 +622,7 @@ import { removeFromArray } from './util/internals'; | |
* @private | ||
* @param {fabric.Object} obj Object that was removed | ||
*/ | ||
_onObjectRemoved: function(obj) { | ||
_onObjectRemoved: function (obj) { | ||
obj._set('canvas', undefined); | ||
this.fire('object:removed', { target: obj }); | ||
obj.fire('removed', { target: this }); | ||
|
@@ -633,7 +634,7 @@ import { removeFromArray } from './util/internals'; | |
* @return {fabric.Canvas} thisArg | ||
* @chainable | ||
*/ | ||
clearContext: function(ctx) { | ||
clearContext: function (ctx) { | ||
ctx.clearRect(0, 0, this.width, this.height); | ||
return this; | ||
}, | ||
|
@@ -674,26 +675,10 @@ import { removeFromArray } from './util/internals'; | |
* @chainable | ||
*/ | ||
renderAll: function () { | ||
var canvasToDrawOn = this.contextContainer; | ||
this.renderCanvas(canvasToDrawOn, this._objects); | ||
this.renderCanvas(this.contextContainer, this._objects); | ||
return this; | ||
}, | ||
|
||
/** | ||
* Function created to be instance bound at initialization | ||
* used in requestAnimationFrame rendering | ||
* Let the fabricJS call it. If you call it manually you could have more | ||
* animationFrame stacking on to of each other | ||
* for an imperative rendering, use canvas.renderAll | ||
* @private | ||
* @return {fabric.Canvas} instance | ||
* @chainable | ||
*/ | ||
renderAndReset: function() { | ||
this.isRendering = 0; | ||
this.renderAll(); | ||
}, | ||
|
||
/** | ||
* Append a renderAll request to next animation frame. | ||
* unless one is already in progress, in that case nothing is done | ||
|
@@ -703,7 +688,25 @@ import { removeFromArray } from './util/internals'; | |
*/ | ||
requestRenderAll: function () { | ||
if (!this.isRendering) { | ||
this.isRendering = fabric.util.requestAnimFrame(this.renderAndResetBound); | ||
let handle; | ||
new Promise((resolve, reject) => { | ||
const controller = new AbortController(); | ||
handle = this.isRendering = fabric.util.requestAnimFrame(resolve); | ||
controller.signal.addEventListener('abort', (e) => { | ||
this.cancelRequestedRender(); | ||
reject(e); | ||
}, { once: true }); | ||
this.__abortControllers[handle] = controller; | ||
}).then(() => { | ||
this.isRendering = 0; | ||
this.renderAll(); | ||
}).catch(error => { | ||
if (error.type !== 'abort') { | ||
throw error; | ||
} | ||
}).finally(() => { | ||
delete this.__abortControllers[handle]; | ||
}) | ||
} | ||
return this; | ||
}, | ||
|
@@ -715,15 +718,15 @@ import { removeFromArray } from './util/internals'; | |
* @return {Object} points.tl | ||
* @chainable | ||
*/ | ||
calcViewportBoundaries: function() { | ||
calcViewportBoundaries: function () { | ||
var width = this.width, height = this.height, | ||
iVpt = invertTransform(this.viewportTransform), | ||
a = transformPoint({ x: 0, y: 0 }, iVpt), | ||
b = transformPoint({ x: width, y: height }, iVpt), | ||
// we don't support vpt flipping | ||
// but the code is robust enough to mostly work with flipping | ||
min = a.min(b), | ||
max = a.max(b); | ||
iVpt = invertTransform(this.viewportTransform), | ||
a = transformPoint({ x: 0, y: 0 }, iVpt), | ||
b = transformPoint({ x: width, y: height }, iVpt), | ||
// we don't support vpt flipping | ||
// but the code is robust enough to mostly work with flipping | ||
min = a.min(b), | ||
max = a.max(b); | ||
return this.vptCoords = { | ||
tl: min, | ||
tr: new Point(max.x, min.y), | ||
|
@@ -732,13 +735,29 @@ import { removeFromArray } from './util/internals'; | |
}; | ||
}, | ||
|
||
cancelRequestedRender: function() { | ||
/** | ||
* consider using {@link abortRendering} to abort concurrent rendering | ||
*/ | ||
cancelRequestedRender: function () { | ||
if (this.isRendering) { | ||
fabric.util.cancelAnimFrame(this.isRendering); | ||
this.isRendering = 0; | ||
} | ||
}, | ||
|
||
/** | ||
* abort concurrent and requested rendering | ||
*/ | ||
abortRendering: function () { | ||
// first cancel requested rendering | ||
this.cancelRequestedRender(); | ||
Object.values(this.__abortControllers) | ||
.forEach(controller => { | ||
controller.abort(); | ||
}); | ||
this.__abortControllers = {}; | ||
}, | ||
|
||
/** | ||
* Renders background, objects, overlay and controls. | ||
* @param {CanvasRenderingContext2D} ctx | ||
|
@@ -748,7 +767,6 @@ import { removeFromArray } from './util/internals'; | |
*/ | ||
renderCanvas: function(ctx, objects) { | ||
var v = this.viewportTransform, path = this.clipPath; | ||
this.cancelRequestedRender(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is an important fix |
||
this.calcViewportBoundaries(); | ||
this.clearContext(ctx); | ||
fabric.util.setImageSmoothing(ctx, this.imageSmoothingEnabled); | ||
|
@@ -1624,11 +1642,7 @@ import { removeFromArray } from './util/internals'; | |
* @chainable | ||
*/ | ||
dispose: function () { | ||
// cancel eventually ongoing renders | ||
if (this.isRendering) { | ||
fabric.util.cancelAnimFrame(this.isRendering); | ||
this.isRendering = 0; | ||
} | ||
this.abortRendering(); | ||
this.forEachObject(function(object) { | ||
object.dispose && object.dispose(); | ||
}); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
polyfill needed for node<16