/
d3_peter10_1.html
116 lines (93 loc) · 2.43 KB
/
d3_peter10_1.html
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
<html lang="en"><head>
<meta charset="UTF-8">
<title>CodePen - translate SVG co-ordinates</title>
<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Lato">
<style>
*, *:before, *:after {
box-sizing: border-box;
padding: 0;
margin: 0;
}
html {
height: 100%;
}
body {
min-height: 100%;
font-family: Lato, sans-serif;
font-size: 100%;
padding: 0;
margin: 10px;
color: #444;
background-color: #fff;
overflow: hidden;
}
svg {
background: radial-gradient(ellipse at center, #fefefe 0%, #cbeeff 100%);
}
rect {
fill: #006da1;
}
circle {
stroke-width: 5;
stroke: #f00;
fill: #ff0;
}
p {
position: absolute;
right: 5px;
bottom: 5px;
padding: 3px 6px;
background-color: rgba(255,255,255,0.8);
}
</style>
</head>
<body translate="no">
<svg id="mysvg" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" viewBox="0 0 1600 800" preserveAspectRatio="xMidYMid meet">
<g id="local" transform="scale(4)">
<rect x="50" y="50" width="100" height="100" rx="10"></rect>
</g>
</svg>
<p id="coords">[page: 825,86] => [svg space: 1399,130] [local transformed space: 350,33]</p>
<script id="rendered-js">
var
svg = document.getElementById('mysvg'),
NS = svg.getAttribute('xmlns'),
local = svg.getElementById('local'),
coords = document.getElementById('coords');
// update co-ordinate output
svg.addEventListener('mousemove', function (e) {
var
x = e.clientX,
y = e.clientY,
svgP = svgPoint(svg, x, y),
svgL = svgPoint(local, x, y);
// output co-ordinates
coords.textContent =
'[page: ' + x + ',' + y +
'] => [svg space: ' + Math.round(svgP.x) + ',' + Math.round(svgP.y) +
'] [local transformed space: ' + Math.round(svgL.x) + ',' + Math.round(svgL.y) + ']';
}, false);
// add a circle to the SVG
svg.addEventListener('click', function (e) {
var
t = e.target,
x = e.clientX,
y = e.clientY,
target = t == svg ? svg : t.parentNode,
svgP = svgPoint(target, x, y),
circle = document.createElementNS(NS, 'circle');
circle.setAttributeNS(null, 'cx', Math.round(svgP.x));
circle.setAttributeNS(null, 'cy', Math.round(svgP.y));
circle.setAttributeNS(null, 'r', 10);
target.appendChild(circle);
}, false);
// translate page to SVG co-ordinate
function svgPoint(element, x, y) {
var pt = svg.createSVGPoint();
pt.x = x;
pt.y = y;
return pt.matrixTransform(element.getScreenCTM().inverse());
}
//# sourceURL=pen.js
</script>
</body></html>