Skip to content

Commit 70faf7e

Browse files
authored
Merge pull request paulrosen#427 from 8633brown/responsive_selection
fix to scale the click coords when paper is set to responsive
2 parents df07262 + 3852533 commit 70faf7e

File tree

1 file changed

+20
-5
lines changed

1 file changed

+20
-5
lines changed

src/write/selection.js

+20-5
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,24 @@ function setupSelection(engraver) {
3434
engraver.renderer.paper.svg.addEventListener('mouseup', mouseUp.bind(engraver));
3535
}
3636

37-
function getCoord(ev) {
38-
var x = ev.offsetX;
39-
var y = ev.offsetY;
37+
function getCoord(ev, svg) {
38+
var scaleX = 1;
39+
var scaleY = 1;
40+
41+
// when renderer.options.responsive === 'resize' the click coords are in relation to the HTML
42+
// element, we need to convert to the SVG viewBox coords
43+
if (svg.viewBox.baseVal) { // Firefox passes null to this when no viewBox is given
44+
// Chrome makes these values null when no viewBox is given.
45+
if (svg.viewBox.baseVal.width !== 0)
46+
scaleX = svg.viewBox.baseVal.width / svg.clientWidth
47+
if (svg.viewBox.baseVal.height !== 0)
48+
scaleY = svg.viewBox.baseVal.height / svg.clientHeight
49+
}
50+
51+
var x = ev.offsetX * scaleX;
52+
var y = ev.offsetY * scaleY;
53+
//console.log(x, y)
54+
4055
// The target might be the SVG that we want, or it could be an item in the SVG (usually a path). If it is not the SVG then
4156
// add an offset to the coordinates.
4257
// if (ev.target.tagName.toLowerCase() !== 'svg') {
@@ -120,7 +135,7 @@ function keyboardSelection(ev) {
120135
function mouseDown(ev) {
121136
// "this" is the EngraverController because of the bind(this) when setting the event listener.
122137

123-
var box = getCoord(ev);
138+
var box = getCoord(ev, this.renderer.paper.svg);
124139
var x = box[0];
125140
var y = box[1];
126141

@@ -180,7 +195,7 @@ function mouseMove(ev) {
180195
if (!this.dragTarget || !this.dragging || !this.dragTarget.isDraggable || this.dragMechanism !== 'mouse')
181196
return;
182197

183-
var box = getCoord(ev);
198+
var box = getCoord(ev, this.renderer.paper.svg);
184199
var x = box[0];
185200
var y = box[1];
186201

0 commit comments

Comments
 (0)