-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
Copy pathdraw.js
180 lines (138 loc) · 5.08 KB
/
draw.js
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
/**
* Copyright 2012-2016, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
var d3 = require('d3');
var Drawing = require('../drawing');
var Axes = require('../../plots/cartesian/axes');
var xmlnsNamespaces = require('../../constants/xmlns_namespaces');
module.exports = function draw(gd) {
var fullLayout = gd._fullLayout,
imageDataAbove = [],
imageDataSubplot = [],
imageDataBelow = [];
if(!fullLayout.images) return;
// Sort into top, subplot, and bottom layers
for(var i = 0; i < fullLayout.images.length; i++) {
var img = fullLayout.images[i];
if(img.layer === 'below' && img.xref !== 'paper' && img.yref !== 'paper') {
imageDataSubplot.push(img);
} else if(img.layer === 'above') {
imageDataAbove.push(img);
} else {
imageDataBelow.push(img);
}
}
var anchors = {
x: {
left: { sizing: 'xMin', offset: 0 },
center: { sizing: 'xMid', offset: -1 / 2 },
right: { sizing: 'xMax', offset: -1 }
},
y: {
top: { sizing: 'YMin', offset: 0 },
middle: { sizing: 'YMid', offset: -1 / 2 },
bottom: { sizing: 'YMax', offset: -1 }
}
};
// Images must be converted to dataURL's for exporting.
function setImage(d) {
var thisImage = d3.select(this);
if(this.img && this.img.src === d.source) {
return;
}
thisImage.attr('xmlns', xmlnsNamespaces.svg);
var imagePromise = new Promise(function(resolve) {
var img = new Image();
this.img = img;
// If not set, a `tainted canvas` error is thrown
img.setAttribute('crossOrigin', 'anonymous');
img.onerror = errorHandler;
img.onload = function() {
var canvas = document.createElement('canvas');
canvas.width = this.width;
canvas.height = this.height;
var ctx = canvas.getContext('2d');
ctx.drawImage(this, 0, 0);
var dataURL = canvas.toDataURL('image/png');
thisImage.attr('xlink:href', dataURL);
};
thisImage.on('error', errorHandler);
thisImage.on('load', resolve);
img.src = d.source;
function errorHandler() {
thisImage.remove();
resolve();
}
}.bind(this));
gd._promises.push(imagePromise);
}
function applyAttributes(d) {
var thisImage = d3.select(this);
// Axes if specified
var xref = Axes.getFromId(gd, d.xref),
yref = Axes.getFromId(gd, d.yref);
var size = fullLayout._size,
width = xref ? Math.abs(xref.l2p(d.sizex) - xref.l2p(0)) : d.sizex * size.w,
height = yref ? Math.abs(yref.l2p(d.sizey) - yref.l2p(0)) : d.sizey * size.h;
// Offsets for anchor positioning
var xOffset = width * anchors.x[d.xanchor].offset,
yOffset = height * anchors.y[d.yanchor].offset;
var sizing = anchors.x[d.xanchor].sizing + anchors.y[d.yanchor].sizing;
// Final positions
var xPos = (xref ? xref.l2p(d.x) + xref._offset : d.x * size.w + size.l) + xOffset,
yPos = (yref ? yref.l2p(d.y) + yref._offset : size.h - d.y * size.h + size.t) + yOffset;
// Construct the proper aspectRatio attribute
switch(d.sizing) {
case 'fill':
sizing += ' slice';
break;
case 'stretch':
sizing = 'none';
break;
}
thisImage.attr({
x: xPos,
y: yPos,
width: width,
height: height,
preserveAspectRatio: sizing,
opacity: d.opacity
});
// Set proper clipping on images
var xId = xref ? xref._id : '',
yId = yref ? yref._id : '',
clipAxes = xId + yId;
if(clipAxes) {
thisImage.call(Drawing.setClipUrl, 'clip' + fullLayout._uid + clipAxes);
}
}
var imagesBelow = fullLayout._imageLowerLayer.selectAll('image')
.data(imageDataBelow),
imagesSubplot = fullLayout._imageSubplotLayer.selectAll('image')
.data(imageDataSubplot),
imagesAbove = fullLayout._imageUpperLayer.selectAll('image')
.data(imageDataAbove);
imagesBelow.enter().append('image');
imagesSubplot.enter().append('image');
imagesAbove.enter().append('image');
imagesBelow.exit().remove();
imagesSubplot.exit().remove();
imagesAbove.exit().remove();
imagesBelow.each(function(d) {
setImage.bind(this)(d);
applyAttributes.bind(this)(d);
});
imagesSubplot.each(function(d) {
setImage.bind(this)(d);
applyAttributes.bind(this)(d);
});
imagesAbove.each(function(d) {
setImage.bind(this)(d);
applyAttributes.bind(this)(d);
});
};