Permalink
Browse files

Fix getOffset for iOS devices

  • Loading branch information...
1 parent a598dab commit 4b19b9755cebaffad68004fcb7218f26e36eb7c3 @padolsey padolsey committed Sep 10, 2012
Showing with 20 additions and 6 deletions.
  1. +1 −1 CHANGELOG
  2. +16 −3 src/renderer/svg/svg.js
  3. +3 −2 test/renderer/svg_renderer_svg-spec.js
View
@@ -1,6 +1,6 @@
v0.3.6
-------------------
-
+* Add additional container DIV in renderer to fix `getOffset` on iOS devices
v0.3.5 / 2012-09-10
-------------------
@@ -86,7 +86,15 @@ define([
}
function Svg(node, width, height) {
+
var root = this.root = this[0] = createElement('svg', 0);
+
+ // We require an additional rootContainer div so we can accurately
+ // retrieve the position of the movie in getOffset on iOS devices.
+ var rootContainer = this.rootContainer = document.createElement('div');
+ rootContainer.style.paddingLeft = '0';
+ rootContainer.style.paddingTop = '0';
+
if (width) {
root.setAttribute('width', width);
}
@@ -98,7 +106,8 @@ define([
this.viewBox(width, height);
this.defs = this.root.appendChild(createElement('defs'));
- node.appendChild(root);
+ rootContainer.appendChild(root);
+ node.appendChild(rootContainer);
}
Svg.prototype = {
@@ -1354,11 +1363,15 @@ define([
proto.getOffset = function() {
+ // We query the bounding box of the rootContainer instead of the root
+ // (i.e. the parent DIV). This is due to an issue with getting the offset
+ // of an SVGElement on iOS devices (unreliable).
+
var ctm,
- offset = this.svg.root.getBoundingClientRect();
+ offset = this.svg.rootContainer.getBoundingClientRect();
if (isNaN(offset.left) || isNaN(offset.top)) {
- ctm = this.svg.root.getScreenCTM();
+ ctm = this.svg.rootContainer.getScreenCTM();
offset.left = ctm.e;
offset.top = ctm.f;
}
@@ -15,10 +15,11 @@ require([
});
describe('the constructor', function() {
- it('creates a svg context and appends it to a given dom element', function() {
+ it('creates an svg container element and appends it to a given dom element', function() {
var el = document.createDocumentFragment();
var svg = new SvgRenderer.Svg(el);
- expect(el.firstChild).toBeInstanceOf(SVGSVGElement);
+ expect(el.firstChild).toBeInstanceOf(HTMLElement);
+ expect(el.firstChild.firstChild).toBeInstanceOf(SVGSVGElement)
});
});

0 comments on commit 4b19b97

Please sign in to comment.