sbweb-scalegorithm
Advanced tools
Comparing version 1.0.0 to 1.1.0
@@ -117,3 +117,3 @@ "use strict"; | ||
var scaleChild = function scaleChild(parent, child, parms) { | ||
var scaleChild = function scaleChild(parent, child, parms, callback) { | ||
// merge parms into defaults | ||
@@ -130,2 +130,7 @@ parms = Object.assign(defaults, parms); | ||
child.style.transform = "scale(" + output.scale + ")"; | ||
/* istanbul ignore else */ | ||
if (callback) { | ||
callback(child); | ||
} | ||
}; | ||
@@ -132,0 +137,0 @@ |
@@ -7,2 +7,2 @@ "use strict"; | ||
* @license MIT | ||
*/var Scalegorithm=function(){var a={mode:"fit",parentSize:null,childSize:null,padding:[0,0],upscaleLimit:0,verbose:!0},r=function(e){if("fit"!=(e=Object.assign(a,e)).mode)throw new Error("scalegorithm invalid mode: currently only 'fit' is supported");if(e.verbose){if(!e.parentSize||2!=e.parentSize.length)throw new Error("scalegorithm parameter invalid: parentSize must be of array[2]");if(e.parentSize[0]<=0||e.parentSize[1]<=0)throw new Error("scalegorithm parameter invalid: parentSize dimensions must both be > 0");if(!e.childSize||2!=e.childSize.length)throw new Error("scalegorithm parameter invalid: childSize must be of array[2]");if(e.childSize[0]<=0||e.childSize[1]<=0)throw new Error("scalegorithm parameter invalid: childSize dimensions must both be > 0");if(!e.padding||2!=e.padding.length)throw new Error("scalegorithm parameter invalid: padding must be of array[2]");if(e.padding[0]<0||e.padding[1]<0)throw new Error("scalegorithm parameter invalid: padding dimensions must both be >= 0")}var i={childTop:null,childLeft:null,scale:1},l=e.parentSize[0]-2*e.padding[0],t=e.parentSize[1]-2*e.padding[1],r=e.childSize[0]/l,o=e.childSize[1]/t;return e.verbose&&(console.log("calc scale with parms",e),console.log("targetscale W H",[1/r,1/o])),i.childTop=1<o?(e.verbose&&console.log("childScale_H > 1"),-e.childSize[1]/2+e.parentSize[1]/2):(e.verbose&&console.log("childScale_H < 1"),null),i.childLeft=1<r?(e.verbose&&console.log("\tchildScale_W > 1"),-e.childSize[0]/2+e.parentSize[0]/2):(e.verbose&&console.log("\tchildScale_W < 1"),null),r<o?(i.scale=1/o,e.verbose&&console.log("\t\tSelected childscale_H",i.scale)):(i.scale=1/r,e.verbose&&console.log("\t\tSelected childScale_W",i.scale)),0<e.upscaleLimit&&i.scale>e.upscaleLimit&&(e.verbose&&console.log("preventing large upscale. new targetscale",e.upscaleLimit),i.scale=e.upscaleLimit),i};return{calc:r,scaleChild:function(e,i,l){(l=Object.assign(a,l)).parentSize=[e.offsetWidth,e.clientHeight],l.childSize=[i.offsetWidth,i.clientHeight];var t=r(l);i.style.position="relative",i.style.top=t.childTop?t.childTop+"px":"",i.style.left=t.childLeft?t.childLeft+"px":"",i.style.transform="scale("+t.scale+")"}}}();"undefined"!=typeof module&&void 0!==module.exports?module.exports=Scalegorithm:"function"==typeof define&&define.amd&&define([],function(){return Scalegorithm}); | ||
*/var Scalegorithm=function(){var a={mode:"fit",parentSize:null,childSize:null,padding:[0,0],upscaleLimit:0,verbose:!0},o=function(e){if("fit"!=(e=Object.assign(a,e)).mode)throw new Error("scalegorithm invalid mode: currently only 'fit' is supported");if(e.verbose){if(!e.parentSize||2!=e.parentSize.length)throw new Error("scalegorithm parameter invalid: parentSize must be of array[2]");if(e.parentSize[0]<=0||e.parentSize[1]<=0)throw new Error("scalegorithm parameter invalid: parentSize dimensions must both be > 0");if(!e.childSize||2!=e.childSize.length)throw new Error("scalegorithm parameter invalid: childSize must be of array[2]");if(e.childSize[0]<=0||e.childSize[1]<=0)throw new Error("scalegorithm parameter invalid: childSize dimensions must both be > 0");if(!e.padding||2!=e.padding.length)throw new Error("scalegorithm parameter invalid: padding must be of array[2]");if(e.padding[0]<0||e.padding[1]<0)throw new Error("scalegorithm parameter invalid: padding dimensions must both be >= 0")}var i={childTop:null,childLeft:null,scale:1},l=e.parentSize[0]-2*e.padding[0],t=e.parentSize[1]-2*e.padding[1],r=e.childSize[0]/l,o=e.childSize[1]/t;return e.verbose&&(console.log("calc scale with parms",e),console.log("targetscale W H",[1/r,1/o])),i.childTop=1<o?(e.verbose&&console.log("childScale_H > 1"),-e.childSize[1]/2+e.parentSize[1]/2):(e.verbose&&console.log("childScale_H < 1"),null),i.childLeft=1<r?(e.verbose&&console.log("\tchildScale_W > 1"),-e.childSize[0]/2+e.parentSize[0]/2):(e.verbose&&console.log("\tchildScale_W < 1"),null),r<o?(i.scale=1/o,e.verbose&&console.log("\t\tSelected childscale_H",i.scale)):(i.scale=1/r,e.verbose&&console.log("\t\tSelected childScale_W",i.scale)),0<e.upscaleLimit&&i.scale>e.upscaleLimit&&(e.verbose&&console.log("preventing large upscale. new targetscale",e.upscaleLimit),i.scale=e.upscaleLimit),i};return{calc:o,scaleChild:function(e,i,l,t){(l=Object.assign(a,l)).parentSize=[e.offsetWidth,e.clientHeight],l.childSize=[i.offsetWidth,i.clientHeight];var r=o(l);i.style.position="relative",i.style.top=r.childTop?r.childTop+"px":"",i.style.left=r.childLeft?r.childLeft+"px":"",i.style.transform="scale("+r.scale+")",t&&t(i)}}}();"undefined"!=typeof module&&void 0!==module.exports?module.exports=Scalegorithm:"function"==typeof define&&define.amd&&define([],function(){return Scalegorithm}); |
{ | ||
"name": "sbweb-scalegorithm", | ||
"version": "1.0.0", | ||
"version": "1.1.0", | ||
"author": "S. Derdeyn <sigged@users.noreply.github.com>", | ||
@@ -5,0 +5,0 @@ "license": "MIT", |
@@ -111,3 +111,3 @@ /** | ||
const scaleChild = function (parent, child, parms) { | ||
const scaleChild = function (parent, child, parms, callback) { | ||
@@ -126,2 +126,7 @@ // merge parms into defaults | ||
child.style.transform = "scale(" + output.scale + ")"; | ||
/* istanbul ignore else */ | ||
if(callback){ | ||
callback(child); | ||
} | ||
} | ||
@@ -128,0 +133,0 @@ |
@@ -58,2 +58,12 @@ const sg = require('../build/scalegorithm.js'); | ||
it('Runs callback and returns child element', () => { | ||
const parms = Object.assign({}, parameters); | ||
const completedHandler = jest.fn(); | ||
sg.scaleChild(mockDom.mockParent, mockDom.mockChild, parms, completedHandler); | ||
expect(completedHandler).toBeCalledWith(mockDom.mockChild); | ||
}); | ||
//todo: create tests for parms.childSize & mockDom.offsetWidth difference | ||
@@ -60,0 +70,0 @@ |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
118582
544