Skip to content

Commit

Permalink
make data-x/y/z supports relative-to-screen size (impress#804)
Browse files Browse the repository at this point in the history
  • Loading branch information
thawk authored Feb 13, 2022
1 parent 20f74a8 commit d3760df
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 71 deletions.
76 changes: 37 additions & 39 deletions js/impress.js
Original file line number Diff line number Diff line change
Expand Up @@ -283,9 +283,9 @@
var data = el.dataset,
step = {
translate: {
x: lib.util.toNumber( data.x ),
y: lib.util.toNumber( data.y ),
z: lib.util.toNumber( data.z )
x: lib.util.toNumberAdvanced( data.x ),
y: lib.util.toNumberAdvanced( data.y ),
z: lib.util.toNumberAdvanced( data.z )
},
rotate: {
x: lib.util.toNumber( data.rotateX ),
Expand Down Expand Up @@ -342,8 +342,7 @@
var init = function() {
if ( initialized ) { return; }

// Initialize configuration object to be used by pre-init plugins.
// Some config may be changed by pre-init plugins.
// Initialize the configuration object, so it can be used by pre-init plugins.
config = buildConfig();
execPreInitPlugins( root );

Expand All @@ -356,10 +355,6 @@
document.head.appendChild( meta );
}

// Initialize configuration object.
// Pre-init plugins may make some change, so we calculate it again.
config = buildConfig();

windowScale = computeWindowScale( config );

// Wrap steps with "canvas" element
Expand Down Expand Up @@ -880,7 +875,7 @@
var thisLevel = preInitPlugins[ i ];
if ( thisLevel !== undefined ) {
for ( var j = 0; j < thisLevel.length; j++ ) {
thisLevel[ j ]( root );
thisLevel[ j ]( root, roots[ "impress-root-" + root.id ] );
}
}
}
Expand Down Expand Up @@ -1253,6 +1248,26 @@
return isNaN( numeric ) ? ( fallback || 0 ) : Number( numeric );
};

/**
* Extends toNumber() to correctly compute also relative-to-screen-size values 5w and 5h.
*
* Returns the computed value in pixels with w/h postfix removed.
*/
var toNumberAdvanced = function( numeric, fallback ) {
if ( typeof numeric !== "string" ) {
return toNumber( numeric, fallback );
}
var ratio = numeric.match( /^([+-]*[\d\.]+)([wh])$/ );
if ( ratio == null ) {
return toNumber( numeric, fallback );
} else {
var value = parseFloat( ratio[ 1 ] );
var config = window.impress.getConfig();
var multiplier = ratio[ 2 ] === "w" ? config.width : config.height;
return value * multiplier;
}
};

// `triggerEvent` builds a custom DOM event with given `eventName` and `detail` data
// and triggers it on element given as `el`.
var triggerEvent = function( el, eventName, detail ) {
Expand All @@ -1269,6 +1284,7 @@
getElementFromHash: getElementFromHash,
throttle: throttle,
toNumber: toNumber,
toNumberAdvanced: toNumberAdvanced,
triggerEvent: triggerEvent,
getUrlParamValue: getUrlParamValue
};
Expand Down Expand Up @@ -3467,7 +3483,9 @@
// Omit steps that are listed as hidden from select widget
if ( hideSteps.indexOf( steps[ i ] ) < 0 ) {
options = options + '<option value="' + steps[ i ].id + '">' + // jshint ignore:line
steps[ i ].id + '</option>' + '\n'; // jshint ignore:line
(
steps[ i ].title ? steps[ i ].title : steps[ i ].id
) + '</option>' + '\n';
}
}
return options;
Expand Down Expand Up @@ -3655,33 +3673,9 @@

var startingState = {};

/**
* Copied from core impress.js. We currently lack a library mechanism to
* to share utility functions like this.
*/
var toNumber = function( numeric, fallback ) {
return isNaN( numeric ) ? ( fallback || 0 ) : Number( numeric );
};

/**
* Extends toNumber() to correctly compute also relative-to-screen-size values 5w and 5h.
*
* Returns the computed value in pixels with w/h postfix removed.
*/
var toNumberAdvanced = function( numeric, fallback ) {
if ( typeof numeric !== "string" ) {
return toNumber( numeric, fallback );
}
var ratio = numeric.match( /^([+-]*[\d\.]+)([wh])$/ );
if ( ratio == null ) {
return toNumber( numeric, fallback );
} else {
var value = parseFloat( ratio[ 1 ] );
var config = window.impress.getConfig();
var multiplier = ratio[ 2 ] === "w" ? config.width : config.height;
return value * multiplier;
}
};
var api;
var toNumber;
var toNumberAdvanced;

var computeRelativePositions = function( el, prev ) {
var data = el.dataset;
Expand Down Expand Up @@ -3755,7 +3749,11 @@
return step;
};

var rel = function( root ) {
var rel = function( root, impressApi ) {
api = impressApi;
toNumber = api.lib.util.toNumber;
toNumberAdvanced = api.lib.util.toNumberAdvanced;

var steps = root.querySelectorAll( ".step" );
var prev;
startingState[ root.id ] = [];
Expand Down
8 changes: 4 additions & 4 deletions src/impress.js
Original file line number Diff line number Diff line change
Expand Up @@ -281,9 +281,9 @@
var data = el.dataset,
step = {
translate: {
x: lib.util.toNumber( data.x ),
y: lib.util.toNumber( data.y ),
z: lib.util.toNumber( data.z )
x: lib.util.toNumberAdvanced( data.x ),
y: lib.util.toNumberAdvanced( data.y ),
z: lib.util.toNumberAdvanced( data.z )
},
rotate: {
x: lib.util.toNumber( data.rotateX ),
Expand Down Expand Up @@ -873,7 +873,7 @@
var thisLevel = preInitPlugins[ i ];
if ( thisLevel !== undefined ) {
for ( var j = 0; j < thisLevel.length; j++ ) {
thisLevel[ j ]( root );
thisLevel[ j ]( root, roots[ "impress-root-" + root.id ] );
}
}
}
Expand Down
21 changes: 21 additions & 0 deletions src/lib/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,26 @@
return isNaN( numeric ) ? ( fallback || 0 ) : Number( numeric );
};

/**
* Extends toNumber() to correctly compute also relative-to-screen-size values 5w and 5h.
*
* Returns the computed value in pixels with w/h postfix removed.
*/
var toNumberAdvanced = function( numeric, fallback ) {
if ( typeof numeric !== "string" ) {
return toNumber( numeric, fallback );
}
var ratio = numeric.match( /^([+-]*[\d\.]+)([wh])$/ );
if ( ratio == null ) {
return toNumber( numeric, fallback );
} else {
var value = parseFloat( ratio[ 1 ] );
var config = window.impress.getConfig();
var multiplier = ratio[ 2 ] === "w" ? config.width : config.height;
return value * multiplier;
}
};

// `triggerEvent` builds a custom DOM event with given `eventName` and `detail` data
// and triggers it on element given as `el`.
var triggerEvent = function( el, eventName, detail ) {
Expand All @@ -97,6 +117,7 @@
getElementFromHash: getElementFromHash,
throttle: throttle,
toNumber: toNumber,
toNumberAdvanced: toNumberAdvanced,
triggerEvent: triggerEvent,
getUrlParamValue: getUrlParamValue
};
Expand Down
36 changes: 8 additions & 28 deletions src/plugins/rel/rel.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,33 +50,9 @@

var startingState = {};

/**
* Copied from core impress.js. We currently lack a library mechanism to
* to share utility functions like this.
*/
var toNumber = function( numeric, fallback ) {
return isNaN( numeric ) ? ( fallback || 0 ) : Number( numeric );
};

/**
* Extends toNumber() to correctly compute also relative-to-screen-size values 5w and 5h.
*
* Returns the computed value in pixels with w/h postfix removed.
*/
var toNumberAdvanced = function( numeric, fallback ) {
if ( typeof numeric !== "string" ) {
return toNumber( numeric, fallback );
}
var ratio = numeric.match( /^([+-]*[\d\.]+)([wh])$/ );
if ( ratio == null ) {
return toNumber( numeric, fallback );
} else {
var value = parseFloat( ratio[ 1 ] );
var config = window.impress.getConfig();
var multiplier = ratio[ 2 ] === "w" ? config.width : config.height;
return value * multiplier;
}
};
var api;
var toNumber;
var toNumberAdvanced;

var computeRelativePositions = function( el, prev ) {
var data = el.dataset;
Expand Down Expand Up @@ -150,7 +126,11 @@
return step;
};

var rel = function( root ) {
var rel = function( root, impressApi ) {
api = impressApi;
toNumber = api.lib.util.toNumber;
toNumberAdvanced = api.lib.util.toNumberAdvanced;

var steps = root.querySelectorAll( ".step" );
var prev;
startingState[ root.id ] = [];
Expand Down

0 comments on commit d3760df

Please sign in to comment.