8000 Merge pull request #6084 from junov/add_willReadFrequently · plotly/plotly.js@9c0ceb9 · GitHub
[go: up one dir, main page]

Skip to content

Commit 9c0ceb9

Browse files
authored
Merge pull request #6084 from junov/add_willReadFrequently
Add willReadFrequently to 2d canvas contexts, where appropriate.
2 parents 8db1646 + 26920a0 commit 9c0ceb9

File tree

7 files changed

+10
-8
lines changed

7 files changed

+10
-8
lines changed

draftlogs/6084_change.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
- Use the willReadFrequently 2d context creation attribute to optimize readback performance [[#6084](https://github.com/plotly/plotly.js/pull/6084)],
2+
with thanks to @junov for the contribution!

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)
0