Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 2k
Expand file tree
/
Copy pathplot.js
More file actions
Latest commit
225 lines (200 loc) · 7.99 KB
/
Copy pathplot.js
File metadata and controls
225 lines (200 loc) · 7.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
'use strict';
vard3=require('@plotly/d3');
varLib=require('../../lib');
varstrTranslate=Lib.strTranslate;
varxmlnsNamespaces=require('../../constants/xmlns_namespaces');
varconstants=require('./constants');
varsupportsPixelatedImage=require('../../lib/supports_pixelated_image');
varPIXELATED_IMAGE_STYLE=require('../../constants/pixelated_image').STYLE;
module.exports=functionplot(gd,plotinfo,cdimage,imageLayer){
varxa=plotinfo.xaxis;
varya=plotinfo.yaxis;
varsupportsPixelated=!gd._context._exportedPlot&&supportsPixelatedImage();
Lib.makeTraceGroups(imageLayer,cdimage,'im').each(function(cd){
varplotGroup=d3.select(this);
varcd0=cd[0];
vartrace=cd0.trace;
varrealImage=(
((trace.zsmooth==='fast')||(trace.zsmooth===false&&supportsPixelated))&&
!trace._hasZ&&trace._hasSource&&xa.type==='linear'&&ya.type==='linear'
);
trace._realImage=realImage;
varz=cd0.z;
varx0=cd0.x0;
vary0=cd0.y0;
varw=cd0.w;
varh=cd0.h;
vardx=trace.dx;
vardy=trace.dy;
varleft,right,temp,top,bottom,i;
// in case of log of a negative
i=0;
while(left===undefined&&i<w){
left=xa.c2p(x0+i*dx);
i++;
}
i=w;
while(right===undefined&&i>0){
right=xa.c2p(x0+i*dx);
i--;
}
i=0;
while(top===undefined&&i<h){
top=ya.c2p(y0+i*dy);
i++;
}
i=h;
while(bottom===undefined&&i>0){
bottom=ya.c2p(y0+i*dy);
i--;
}
if(right<left){
temp=right;
right=left;
left=temp;
}
if(bottom<top){
temp=top;
top=bottom;
bottom=temp;
}
// Reduce image size when zoomed in to save memory
if(!realImage){
varextra=0.5;// half the axis size
left=Math.max(-extra*xa._length,left);
right=Math.min((1+extra)*xa._length,right);
top=Math.max(-extra*ya._length,top);
bottom=Math.min((1+extra)*ya._length,bottom);
}
varimageWidth=Math.round(right-left);
varimageHeight=Math.round(bottom-top);
// if image is entirely off-screen, don't even draw it
varisOffScreen=(imageWidth<=0||imageHeight<=0);
if(isOffScreen){
varnoImage=plotGroup.selectAll('image').data([]);
noImage.exit().remove();
return;
}
// Create a new canvas and draw magnified pixels on it
functiondrawMagnifiedPixelsOnCanvas(readPixel){
varcanvas=document.createElement('canvas');
canvas.width=imageWidth;
canvas.height=imageHeight;
varcontext=canvas.getContext('2d',{willReadFrequently: true});
varipx=function(i){returnLib.constrain(Math.round(xa.c2p(x0+i*dx)-left),0,imageWidth);};
varjpx=function(j){returnLib.constrain(Math.round(ya.c2p(y0+j*dy)-top),0,imageHeight);};
varcr=constants.colormodel[trace.colormodel];
varcolormodel=(cr.colormodel||trace.colormodel);
varfmt=cr.fmt;
varc;
for(i=0;i<cd0.w;i++){
varipx0=ipx(i);varipx1=ipx(i+1);
if(ipx1===ipx0||isNaN(ipx1)||isNaN(ipx0))continue;
for(varj=0;j<cd0.h;j++){
varjpx0=jpx(j);varjpx1=jpx(j+1);
if(jpx1===jpx0||isNaN(jpx1)||isNaN(jpx0)||!readPixel(i,j))continue;
c=trace._scaler(readPixel(i,j));
if(c){
context.fillStyle=colormodel+'('+fmt(c).join(',')+')';
}else{
// Return a transparent pixel
context.fillStyle='rgba(0,0,0,0)';
}
context.fillRect(ipx0,jpx0,ipx1-ipx0,jpx1-jpx0);
}
}
returncanvas;
}
varimage3=plotGroup.selectAll('image')
.data([cd]);
image3.enter().append('svg:image').attr({
xmlns: xmlnsNamespaces.svg,
preserveAspectRatio: 'none'
});
image3.exit().remove();
varstyle=(trace.zsmooth===false) ? PIXELATED_IMAGE_STYLE : '';
if(realImage){
varxRange=Lib.simpleMap(xa.range,xa.r2l);
varyRange=Lib.simpleMap(ya.range,ya.r2l);
varflipX=xRange[1]<xRange[0];
varflipY=yRange[1]>yRange[0];
if(flipX||flipY){
vartx=left+imageWidth/2;
varty=top+imageHeight/2;
style+='transform:'+
strTranslate(tx+'px',ty+'px')+
'scale('+(flipX ? -1 : 1)+','+(flipY ? -1 : 1)+')'+
strTranslate(-tx+'px',-ty+'px')+';';
}
}
image3.attr('style',style);
varp=newPromise(function(resolve){
if(trace._hasZ){
resolve();
}elseif(trace._hasSource){
// Check if canvas already exists and has the right data
if(
trace._canvas&&
trace._canvas.el.width===w&&
trace._canvas.el.height===h&&
trace._canvas.source===trace.source
){
resolve();
}else{
// Create a canvas and transfer image onto it to access pixel information
varcanvas=document.createElement('canvas');
canvas.width=w;
canvas.height=h;
varcontext=canvas.getContext('2d',{willReadFrequently: true});
trace._image=trace._image||newImage();
varimage=trace._image;
image.onload=function(){
context.drawImage(image,0,0);
trace._canvas={
el: canvas,
source: trace.source
};
resolve();
};
image.setAttribute('src',trace.source);
}
}
})
.then(function(){
varhref,canvas;
if(trace._hasZ){
canvas=drawMagnifiedPixelsOnCanvas(function(i,j){
var_z=z[j][i];
if(Lib.isTypedArray(_z))_z=Array.from(_z);
return_z;
});
href=canvas.toDataURL('image/png');
}elseif(trace._hasSource){
if(realImage){
href=trace.source;
}else{
varcontext=trace._canvas.el.getContext('2d',{willReadFrequently: true});
vardata=context.getImageData(0,0,w,h).data;
canvas=drawMagnifiedPixelsOnCanvas(function(i,j){
varindex=4*(j*w+i);
return[
data[index],
data[index+1],
data[index+2],
data[index+3]
];
});
href=canvas.toDataURL('image/png');
}
}
image3.attr({
'xlink:href': href,
height: imageHeight,
width: imageWidth,
x: left,
y: top
});
});
gd._promises.push(p);
});
};