From 3ab58da4427a49b80b7ae5de557f2413a84b92a6 Mon Sep 17 00:00:00 2001 From: maxwellito Date: Wed, 13 Jun 2018 00:50:11 +0100 Subject: [PATCH] v0.4.4 Fix chrome 67 (#199) * Use XMLHttpRequest to fetch SVGs * v0.4.4 --- dist/vivus.js | 41 +++++++++++++++++++++++++++++------------ dist/vivus.min.js | 4 ++-- package.json | 2 +- src/vivus.js | 39 ++++++++++++++++++++++++++++----------- test/unit/vivus.spec.js | 3 ++- 5 files changed, 62 insertions(+), 27 deletions(-) diff --git a/dist/vivus.js b/dist/vivus.js index e33e15c..ad3ddba 100644 --- a/dist/vivus.js +++ b/dist/vivus.js @@ -1,6 +1,6 @@ /** * vivus - JavaScript library to make drawing animation on SVG - * @version v0.4.2 + * @version v0.4.4 * @link https://github.com/maxwellito/vivus * @license MIT */ @@ -387,6 +387,8 @@ Vivus.EASE_OUT_BOUNCE = function (x) { * @param {DOM|String} element SVG Dom element or id of it */ Vivus.prototype.setElement = function (element, options) { + var onLoad, self; + // Basic check if (typeof element === 'undefined') { throw new Error('Vivus [constructor]: "element" parameter is required'); @@ -401,14 +403,32 @@ Vivus.prototype.setElement = function (element, options) { } this.parentEl = element; - // Create the object element if the property `file` exists in the options object + // Load the SVG with XMLHttpRequest and extract the SVG if (options && options.file) { - var objElm = document.createElement('object'); - objElm.setAttribute('type', 'image/svg+xml'); - objElm.setAttribute('data', options.file); - objElm.setAttribute('built-by-vivus', 'true'); - element.appendChild(objElm); - element = objElm; + var self = this; + onLoad = function (e) { + var domSandbox = document.createElement('div'); + domSandbox.innerHTML = this.responseText; + + var svgTag = domSandbox.querySelector('svg'); + if (!svgTag) { + throw new Error('Vivus [load]: Cannot find the SVG in the loaded file : ' + options.file); + } + + self.el = svgTag + self.el.setAttribute('width', '100%'); + self.el.setAttribute('height', '100%'); + self.parentEl.appendChild(self.el) + self.isReady = true; + self.init(); + self = null; + } + + var oReq = new window.XMLHttpRequest(); + oReq.addEventListener('load', onLoad); + oReq.open('GET', options.file); + oReq.send(); + return; } switch (element.constructor) { @@ -420,9 +440,6 @@ Vivus.prototype.setElement = function (element, options) { break; case window.HTMLObjectElement: - // If we have to wait for it - var onLoad, self; - self = this; onLoad = function (e) { if (self.isReady) { @@ -441,7 +458,7 @@ Vivus.prototype.setElement = function (element, options) { } self.isReady = true; self.init(); - return true; + self = null; } }; diff --git a/dist/vivus.min.js b/dist/vivus.min.js index 68977de..4e0e5a7 100644 --- a/dist/vivus.min.js +++ b/dist/vivus.min.js @@ -1,7 +1,7 @@ /** * vivus - JavaScript library to make drawing animation on SVG - * @version v0.4.2 + * @version v0.4.4 * @link https://github.com/maxwellito/vivus * @license MIT */ -"use strict";!function(){function t(t){if("undefined"==typeof t)throw new Error('Pathformer [constructor]: "element" parameter is required');if(t.constructor===String&&(t=document.getElementById(t),!t))throw new Error('Pathformer [constructor]: "element" parameter is not related to an existing ID');if(!(t instanceof window.SVGElement||t instanceof window.SVGGElement||/^svg$/i.test(t.nodeName)))throw new Error('Pathformer [constructor]: "element" parameter must be a string or a SVGelement');this.el=t,this.scan(t)}function e(t,e,n){r(),this.isReady=!1,this.setElement(t,e),this.setOptions(e),this.setCallback(n),this.isReady&&this.init()}t.prototype.TYPES=["line","ellipse","circle","polygon","polyline","rect"],t.prototype.ATTR_WATCH=["cx","cy","points","r","rx","ry","x","x1","x2","y","y1","y2"],t.prototype.scan=function(t){for(var e,r,n,i,a=t.querySelectorAll(this.TYPES.join(",")),o=0;oo?s:o,0),i/2),s=Math.min(Math.max(0>s?o:s,0),a/2),e.d="M "+(r+o)+","+n+" L "+(r+i-o)+","+n+" A "+o+","+s+",0,0,1,"+(r+i)+","+(n+s)+" L "+(r+i)+","+(n+a-s)+" A "+o+","+s+",0,0,1,"+(r+i-o)+","+(n+a)+" L "+(r+o)+","+(n+a)+" A "+o+","+s+",0,0,1,"+r+","+(n+a-s)+" L "+r+","+(n+s)+" A "+o+","+s+",0,0,1,"+(r+o)+","+n}else e.d="M"+r+" "+n+" L"+(r+i)+" "+n+" L"+(r+i)+" "+(n+a)+" L"+r+" "+(n+a)+" Z";return e},t.prototype.polylineToPath=function(t){var e,r,n={},i=t.points.trim().split(" ");if(-1===t.points.indexOf(",")){var a=[];for(e=0;e=this.duration)throw new Error("Vivus [constructor]: delay must be shorter than duration")},e.prototype.setCallback=function(t){if(t&&t.constructor!==Function)throw new Error('Vivus [constructor]: "callback" parameter must be a function');this.callback=t||function(){}},e.prototype.mapping=function(){var t,e,r,n,i,o,s,h;for(h=o=s=0,e=this.el.querySelectorAll("path"),t=0;t1?e.length-1:1),this.reverseStack&&this.map.reverse(),t=0;t=this.frameLength))return this.trace(),this.handle=n(function(){t.drawer()}),void 0;this.stop(),this.currentFrame=this.frameLength,this.trace(),this.selfDestroy&&this.destroy()}this.callback(this),this.instanceCallback&&(this.instanceCallback(this),this.instanceCallback=null)},e.prototype.trace=function(){var t,e,r,n;for(n=this.animTimingFunction(this.currentFrame/this.frameLength)*this.frameLength,t=0;t=o+a*e&&s>=r},e.prototype.getViewportH=function(){var t=this.docElem.clientHeight,e=window.innerHeight;return e>t?e:t},e.prototype.scrollY=function(){return window.pageYOffset||this.docElem.scrollTop},r=function(){e.prototype.docElem||(e.prototype.docElem=window.document.documentElement,n=function(){return window.requestAnimationFrame||window.webkitRequestAnimationFrame||window.mozRequestAnimationFrame||window.oRequestAnimationFrame||window.msRequestAnimationFrame||function(t){return window.setTimeout(t,1e3/60)}}(),i=function(){return window.cancelAnimationFrame||window.webkitCancelAnimationFrame||window.mozCancelAnimationFrame||window.oCancelAnimationFrame||window.msCancelAnimationFrame||function(t){return window.clearTimeout(t)}}())},a=function(t,e){var r=parseInt(t,10);return r>=0?r:e},"function"==typeof define&&define.amd?define([],function(){return e}):"object"==typeof exports?module.exports=e:window.Vivus=e}(); \ No newline at end of file +"use strict";!function(){function t(t){if("undefined"==typeof t)throw new Error('Pathformer [constructor]: "element" parameter is required');if(t.constructor===String&&(t=document.getElementById(t),!t))throw new Error('Pathformer [constructor]: "element" parameter is not related to an existing ID');if(!(t instanceof window.SVGElement||t instanceof window.SVGGElement||/^svg$/i.test(t.nodeName)))throw new Error('Pathformer [constructor]: "element" parameter must be a string or a SVGelement');this.el=t,this.scan(t)}function e(t,e,n){r(),this.isReady=!1,this.setElement(t,e),this.setOptions(e),this.setCallback(n),this.isReady&&this.init()}t.prototype.TYPES=["line","ellipse","circle","polygon","polyline","rect"],t.prototype.ATTR_WATCH=["cx","cy","points","r","rx","ry","x","x1","x2","y","y1","y2"],t.prototype.scan=function(t){for(var e,r,n,i,a=t.querySelectorAll(this.TYPES.join(",")),o=0;oo?s:o,0),i/2),s=Math.min(Math.max(0>s?o:s,0),a/2),e.d="M "+(r+o)+","+n+" L "+(r+i-o)+","+n+" A "+o+","+s+",0,0,1,"+(r+i)+","+(n+s)+" L "+(r+i)+","+(n+a-s)+" A "+o+","+s+",0,0,1,"+(r+i-o)+","+(n+a)+" L "+(r+o)+","+(n+a)+" A "+o+","+s+",0,0,1,"+r+","+(n+a-s)+" L "+r+","+(n+s)+" A "+o+","+s+",0,0,1,"+(r+o)+","+n}else e.d="M"+r+" "+n+" L"+(r+i)+" "+n+" L"+(r+i)+" "+(n+a)+" L"+r+" "+(n+a)+" Z";return e},t.prototype.polylineToPath=function(t){var e,r,n={},i=t.points.trim().split(" ");if(-1===t.points.indexOf(",")){var a=[];for(e=0;e=this.duration)throw new Error("Vivus [constructor]: delay must be shorter than duration")},e.prototype.setCallback=function(t){if(t&&t.constructor!==Function)throw new Error('Vivus [constructor]: "callback" parameter must be a function');this.callback=t||function(){}},e.prototype.mapping=function(){var t,e,r,n,i,o,s,h;for(h=o=s=0,e=this.el.querySelectorAll("path"),t=0;t1?e.length-1:1),this.reverseStack&&this.map.reverse(),t=0;t=this.frameLength))return this.trace(),this.handle=n(function(){t.drawer()}),void 0;this.stop(),this.currentFrame=this.frameLength,this.trace(),this.selfDestroy&&this.destroy()}this.callback(this),this.instanceCallback&&(this.instanceCallback(this),this.instanceCallback=null)},e.prototype.trace=function(){var t,e,r,n;for(n=this.animTimingFunction(this.currentFrame/this.frameLength)*this.frameLength,t=0;t=o+a*e&&s>=r},e.prototype.getViewportH=function(){var t=this.docElem.clientHeight,e=window.innerHeight;return e>t?e:t},e.prototype.scrollY=function(){return window.pageYOffset||this.docElem.scrollTop},r=function(){e.prototype.docElem||(e.prototype.docElem=window.document.documentElement,n=function(){return window.requestAnimationFrame||window.webkitRequestAnimationFrame||window.mozRequestAnimationFrame||window.oRequestAnimationFrame||window.msRequestAnimationFrame||function(t){return window.setTimeout(t,1e3/60)}}(),i=function(){return window.cancelAnimationFrame||window.webkitCancelAnimationFrame||window.mozCancelAnimationFrame||window.oCancelAnimationFrame||window.msCancelAnimationFrame||function(t){return window.clearTimeout(t)}}())},a=function(t,e){var r=parseInt(t,10);return r>=0?r:e},"function"==typeof define&&define.amd?define([],function(){return e}):"object"==typeof exports?module.exports=e:window.Vivus=e}(); \ No newline at end of file diff --git a/package.json b/package.json index cbfcd80..a7bb99e 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "vivus", - "version": "0.4.3", + "version": "0.4.4", "description": "JavaScript library to make drawing animation on SVG", "main": "dist/vivus.js", "scripts": { diff --git a/src/vivus.js b/src/vivus.js index 63a17f9..0c0ca62 100755 --- a/src/vivus.js +++ b/src/vivus.js @@ -99,6 +99,8 @@ Vivus.EASE_OUT_BOUNCE = function (x) { * @param {DOM|String} element SVG Dom element or id of it */ Vivus.prototype.setElement = function (element, options) { + var onLoad, self; + // Basic check if (typeof element === 'undefined') { throw new Error('Vivus [constructor]: "element" parameter is required'); @@ -113,14 +115,32 @@ Vivus.prototype.setElement = function (element, options) { } this.parentEl = element; - // Create the object element if the property `file` exists in the options object + // Load the SVG with XMLHttpRequest and extract the SVG if (options && options.file) { - var objElm = document.createElement('object'); - objElm.setAttribute('type', 'image/svg+xml'); - objElm.setAttribute('data', options.file); - objElm.setAttribute('built-by-vivus', 'true'); - element.appendChild(objElm); - element = objElm; + var self = this; + onLoad = function (e) { + var domSandbox = document.createElement('div'); + domSandbox.innerHTML = this.responseText; + + var svgTag = domSandbox.querySelector('svg'); + if (!svgTag) { + throw new Error('Vivus [load]: Cannot find the SVG in the loaded file : ' + options.file); + } + + self.el = svgTag + self.el.setAttribute('width', '100%'); + self.el.setAttribute('height', '100%'); + self.parentEl.appendChild(self.el) + self.isReady = true; + self.init(); + self = null; + } + + var oReq = new window.XMLHttpRequest(); + oReq.addEventListener('load', onLoad); + oReq.open('GET', options.file); + oReq.send(); + return; } switch (element.constructor) { @@ -132,9 +152,6 @@ Vivus.prototype.setElement = function (element, options) { break; case window.HTMLObjectElement: - // If we have to wait for it - var onLoad, self; - self = this; onLoad = function (e) { if (self.isReady) { @@ -153,7 +170,7 @@ Vivus.prototype.setElement = function (element, options) { } self.isReady = true; self.init(); - return true; + self = null; } }; diff --git a/test/unit/vivus.spec.js b/test/unit/vivus.spec.js index 7e2b7d2..42278de 100644 --- a/test/unit/vivus.spec.js +++ b/test/unit/vivus.spec.js @@ -155,12 +155,13 @@ describe('Vivus', function () { it('should accept any DOM element if `file` option is set', function () { var divTag = document.createElement('div'); + spyOn(window, 'XMLHttpRequest'); try { new Vivus(divTag, {file: 'opensource.svg'}); } catch(err) {} - expect(!!divTag.querySelector('object')).toBe(true); + expect(window.XMLHttpRequest).toHaveBeenCalled(); }); it('should throw an error if the element is not a correct type (DOM object or string)', function () {