Skip to content
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

Add willReadFrequently to 2d canvas contexts, where appropriate. #6084

Merged
merged 4 commits into from
Mar 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions draftlogs/6084_change.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
- Use the willReadFrequently 2d context creation attribute to optimize readback performance [[#6084](https://github.com/plotly/plotly.js/pull/6084)],
with thanks to @junov for the contribution!
2 changes: 1 addition & 1 deletion src/components/images/draw.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ module.exports = function draw(gd) {
canvas.width = this.width;
canvas.height = this.height;

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

var dataURL = canvas.toDataURL('image/png');
Expand Down
2 changes: 1 addition & 1 deletion src/plots/gl2d/scene2d.js
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ proto.toImage = function(format) {
canvas.width = w;
canvas.height = h;

var context = canvas.getContext('2d');
var context = canvas.getContext('2d', {willReadFrequently: true});
var imageData = context.createImageData(w, h);
imageData.data.set(pixels);
context.putImageData(imageData, 0, 0);
Expand Down
2 changes: 1 addition & 1 deletion src/plots/gl3d/scene.js
Original file line number Diff line number Diff line change
Expand Up @@ -1084,7 +1084,7 @@ proto.toImage = function(format) {
var canvas = document.createElement('canvas');
canvas.width = w;
canvas.height = h;
var context = canvas.getContext('2d');
var context = canvas.getContext('2d', {willReadFrequently: true});
var imageData = context.createImageData(w, h);
imageData.data.set(pixels);
context.putImageData(imageData, 0, 0);
Expand Down
2 changes: 1 addition & 1 deletion src/snapshot/svgtoimg.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ function svgToImg(opts) {
var w1 = scale * w0;
var h1 = scale * h0;

var ctx = canvas.getContext('2d');
var ctx = canvas.getContext('2d', {willReadFrequently: true});
var img = new Image();
var svgBlob, url;

Expand Down
2 changes: 1 addition & 1 deletion src/traces/image/hover.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ module.exports = function hoverPoints(pointData, xval, yval) {
if(trace._hasZ) {
pixel = cd0.z[ny][nx];
} else if(trace._hasSource) {
pixel = trace._canvas.el.getContext('2d').getImageData(nx, ny, 1, 1).data;
pixel = trace._canvas.el.getContext('2d', {willReadFrequently: true}).getImageData(nx, ny, 1, 1).data;
}

// return early if pixel is undefined
Expand Down
6 changes: 3 additions & 3 deletions src/traces/image/plot.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ module.exports = function plot(gd, plotinfo, cdimage, imageLayer) {
var canvas = document.createElement('canvas');
canvas.width = imageWidth;
canvas.height = imageHeight;
var context = canvas.getContext('2d');
var context = canvas.getContext('2d', {willReadFrequently: true});

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

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