Skip to content
Permalink
Browse files
Merge pull request #6084 from junov/add_willReadFrequently
Add willReadFrequently to 2d canvas contexts, where appropriate.
  • Loading branch information
archmoj committed Mar 24, 2022
2 parents 8db1646 + 26920a0 commit 9c0ceb9ed79b06076867b3d4e5b5c00c779ccbcb
Showing with 10 additions and 8 deletions.
  1. +2 −0 draftlogs/6084_change.md
  2. +1 −1 src/components/images/draw.js
  3. +1 −1 src/plots/gl2d/scene2d.js
  4. +1 −1 src/plots/gl3d/scene.js
  5. +1 −1 src/snapshot/svgtoimg.js
  6. +1 −1 src/traces/image/hover.js
  7. +3 −3 src/traces/image/plot.js
@@ -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!
@@ -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');
@@ -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);
@@ -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);
@@ -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;

@@ -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
@@ -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);};
@@ -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;
@@ -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);

0 comments on commit 9c0ceb9

Please sign in to comment.