Skip to content

Commit 28a44b3

Browse files
committed
Images: Add draw method
1 parent a9d9860 commit 28a44b3

File tree

2 files changed

+127
-0
lines changed

2 files changed

+127
-0
lines changed

src/components/images/draw.js

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
/**
2+
* Copyright 2012-2016, Plotly, Inc.
3+
* All rights reserved.
4+
*
5+
* This source code is licensed under the MIT license found in the
6+
* LICENSE file in the root directory of this source tree.
7+
*/
8+
9+
'use strict';
10+
11+
var d3 = require('d3');
12+
var Drawing = require('../drawing');
13+
var Axes = require('../../plots/cartesian/axes');
14+
15+
module.exports = function draw(gd) {
16+
17+
var fullLayout = gd._fullLayout,
18+
imageDataAbove = [],
19+
imageDataSubplot = [],
20+
imageDataBelow = [];
21+
22+
if(!fullLayout.images) return;
23+
24+
25+
// Sort into top, subplot, and bottom layers
26+
for(var i = 0; i < fullLayout.images.length; i++) {
27+
var img = fullLayout.images[i];
28+
29+
if(img.layer === 'below' && img.xref !== 'paper' && img.yref !== 'paper') {
30+
imageDataSubplot.push(img);
31+
} else if(img.layer === 'above') {
32+
imageDataAbove.push(img);
33+
} else {
34+
imageDataBelow.push(img);
35+
}
36+
}
37+
38+
39+
var anchors = {
40+
x: {
41+
left: { sizing: 'xMin', offset: 0 },
42+
center: { sizing: 'xMid', offset: -1 / 2 },
43+
right: { sizing: 'xMax', offset: -1 }
44+
},
45+
y: {
46+
top: { sizing: 'YMin', offset: 0 },
47+
middle: { sizing: 'YMid', offset: -1 / 2 },
48+
bottom: { sizing: 'YMax', offset: -1 }
49+
}
50+
};
51+
52+
53+
function applyAttributes(d) {
54+
55+
var thisImage = d3.select(this);
56+
57+
// Axes if specified
58+
var xref = Axes.getFromId(gd, d.xref),
59+
yref = Axes.getFromId(gd, d.yref);
60+
61+
var size = fullLayout._size,
62+
width = xref ? Math.abs(xref.l2p(d.width) - xref.l2p(0)) : d.width * size.w,
63+
height = yref ? Math.abs(yref.l2p(d.height) - yref.l2p(0)) : d.width * size.h;
64+
65+
// Offsets for anchor positioning
66+
var xOffset = width * anchors.x[d.xanchor].offset + size.l,
67+
yOffset = height * anchors.y[d.yanchor].offset + size.t;
68+
69+
var sizing = anchors.x[d.xanchor].sizing + anchors.y[d.yanchor].sizing;
70+
71+
// Final positions
72+
var xPos = (xref ? xref.l2p(d.x) : d.x * width) + xOffset,
73+
yPos = (yref ? yref.l2p(d.y) : -d.y * width) + yOffset;
74+
75+
76+
// Construct the proper aspectRatio attribute
77+
switch(d.sizing) {
78+
case 'fill':
79+
sizing += ' slice';
80+
break;
81+
82+
case 'stretch':
83+
sizing = 'none';
84+
break;
85+
}
86+
87+
thisImage.attr({
88+
href: d.source,
89+
x: xPos,
90+
y: yPos,
91+
width: width,
92+
height: height,
93+
preserveAspectRatio: sizing,
94+
opacity: d.opacity
95+
});
96+
97+
98+
// Set proper clipping on images
99+
var xId = xref ? xref._id : '',
100+
yId = yref ? yref._id : '',
101+
clipAxes = xId + yId;
102+
103+
thisImage.call(Drawing.setClipUrl, 'clip' + fullLayout._uid + clipAxes);
104+
}
105+
106+
107+
var imagesBelow = fullLayout._imageLowerLayer.selectAll('image')
108+
.data(imageDataBelow),
109+
imagesSubplot = fullLayout._imageSubplotLayer.selectAll('image')
110+
.data(imageDataSubplot),
111+
imagesAbove = fullLayout._imageUpperLayer.selectAll('image')
112+
.data(imageDataAbove);
113+
114+
imagesBelow.enter().append('image');
115+
imagesSubplot.enter().append('image');
116+
imagesAbove.enter().append('image');
117+
118+
imagesBelow.exit().remove();
119+
imagesSubplot.exit().remove();
120+
imagesAbove.exit().remove();
121+
122+
imagesBelow.each(applyAttributes);
123+
imagesSubplot.each(applyAttributes);
124+
imagesAbove.each(applyAttributes);
125+
};

src/components/images/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,11 @@
99
'use strict';
1010

1111

12+
var draw = require('./draw');
1213
var supplyLayoutDefaults = require('./defaults');
1314

1415

1516
module.exports = {
17+
draw: draw,
1618
supplyLayoutDefaults: supplyLayoutDefaults
1719
};

0 commit comments

Comments
 (0)