Skip to content

Commit e416343

Browse files
committed
Add willReadFrequently to 2d canvas contexts, where appropriate.
This chang add the willReadFrequently:true 2d context creation attribute to call sites where canvases will be read from (i.e getImagData or toDataURL). This is a performance hint that tells the browser to optimize for readbacks. See: https://html.spec.whatwg.org/multipage/canvas.html#concept-canvas-will-read-frequently The new API will soon be launched in chromium-based web browsers. The objective of this change is to optimize performance and prevent possible performance regressions when this new feature is launched by major web browser in the near future.
1 parent d971001 commit e416343

File tree

6 files changed

+8
-8
lines changed

6 files changed

+8
-8
lines changed

src/components/images/draw.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ module.exports = function draw(gd) {
8989
canvas.width = this.width;
9090
canvas.height = this.height;
9191

92-
var ctx = canvas.getContext('2d');
92+
var ctx = canvas.getContext('2d', {willReadFrequently: true});
9393
ctx.drawImage(this, 0, 0);
9494

9595
var dataURL = canvas.toDataURL('image/png');

src/plots/gl2d/scene2d.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ proto.toImage = function(format) {
211211
canvas.width = w;
212212
canvas.height = h;
213213

214-
var context = canvas.getContext('2d');
214+
var context = canvas.getContext('2d', {willReadFrequently: true});
215215
var imageData = context.createImageData(w, h);
216216
imageData.data.set(pixels);
217217
context.putImageData(imageData, 0, 0);

src/plots/gl3d/scene.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1084,7 +1084,7 @@ proto.toImage = function(format) {
10841084
var canvas = document.createElement('canvas');
10851085
canvas.width = w;
10861086
canvas.height = h;
1087-
var context = canvas.getContext('2d');
1087+
var context = canvas.getContext('2d', {willReadFrequently: true});
10881088
var imageData = context.createImageData(w, h);
10891089
imageData.data.set(pixels);
10901090
context.putImageData(imageData, 0, 0);

src/snapshot/svgtoimg.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ function svgToImg(opts) {
3333
var w1 = scale * w0;
3434
var h1 = scale * h0;
3535

36-
var ctx = canvas.getContext('2d');
36+
var ctx = canvas.getContext('2d', {willReadFrequently: true});
3737
var img = new Image();
3838
var svgBlob, url;
3939

src/traces/image/hover.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ module.exports = function hoverPoints(pointData, xval, yval) {
2424
if(trace._hasZ) {
2525
pixel = cd0.z[ny][nx];
2626
} else if(trace._hasSource) {
27-
pixel = trace._canvas.el.getContext('2d').getImageData(nx, ny, 1, 1).data;
27+
pixel = trace._canvas.el.getContext('2d', {willReadFrequently: true}).getImageData(nx, ny, 1, 1).data;
2828
}
2929

3030
// return early if pixel is undefined

src/traces/image/plot.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ module.exports = function plot(gd, plotinfo, cdimage, imageLayer) {
9292
var canvas = document.createElement('canvas');
9393
canvas.width = imageWidth;
9494
canvas.height = imageHeight;
95-
var context = canvas.getContext('2d');
95+
var context = canvas.getContext('2d', {willReadFrequently: true});
9696

9797
var ipx = function(i) {return Lib.constrain(Math.round(xa.c2p(x0 + i * dx) - left), 0, imageWidth);};
9898
var jpx = function(j) {return Lib.constrain(Math.round(ya.c2p(y0 + j * dy) - top), 0, imageHeight);};
@@ -167,7 +167,7 @@ module.exports = function plot(gd, plotinfo, cdimage, imageLayer) {
167167
var canvas = document.createElement('canvas');
168168
canvas.width = w;
169169
canvas.height = h;
170-
var context = canvas.getContext('2d');
170+
var context = canvas.getContext('2d', {willReadFrequently: true});
171171

172172
trace._image = trace._image || new Image();
173173
var image = trace._image;
@@ -192,7 +192,7 @@ module.exports = function plot(gd, plotinfo, cdimage, imageLayer) {
192192
if(realImage) {
193193
href = trace.source;
194194
} else {
195-
var context = trace._canvas.el.getContext('2d');
195+
var context = trace._canvas.el.getContext('2d', {willReadFrequently:true});
196196
var data = context.getImageData(0, 0, w, h).data;
197197
canvas = drawMagnifiedPixelsOnCanvas(function(i, j) {
198198
var index = 4 * (j * w + i);

0 commit comments

Comments
 (0)