Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added dirty way of setting what the result should be AFTER the spinni… #92

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 9 additions & 2 deletions examples/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,16 @@

<body>
<div class="js-bounty"></div>
<script src="/bounty.js"></script>
<div onclick="setTo('works');" style="width: 100px; height:100px; background-color: 'red';">click here</div>
<div class="js-bounty1"></div>
<script src="../lib/bounty.js"></script>
<script>
bounty.default({ el: '.js-bounty', value: '£144,167.60' })
const {setTo} = bounty.default({ el: '.js-bounty', value: 'dark', initialValue: "Matching...",
animationDelay: 100, letterAnimationDelay: 100, continuousRun: true })
</script>
<script>
//bounty.default({ el: '.js-bounty1', value: 'montgomery21', initialValue: "Matching...",
//animationDelay: 100, letterAnimationDelay: 100 })
</script>
</body>

Expand Down
121 changes: 91 additions & 30 deletions src/bounty.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,28 @@ import loop from "./loop";
import { select, append, attr, style, text } from "./selection";
import transition from "./transition";

const DIGITS_COUNT = 10;

const ROTATIONS = 3;
var ALL_CHARS = "";//"0123456789ABCDEFGHIJKLMNOPRSTUVWXYZabcdefghijklmnoprstuvwxyz.";

for( var i = 32; i <= 126; i++ )
{
ALL_CHARS += String.fromCharCode( i );
}
//include the non breaking space;
ALL_CHARS += String.fromCharCode("\u00a0");

//console.log(ALL_CHARS[ALL_CHARS.length-1]);
const DIGITS_COUNT = ALL_CHARS.length;

const createDigitRoulette = (svg, fontSize, lineHeight, id) => {
const digits = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0];
const createDigitRoulette = (svg, fontSize, lineHeight, id, noBlur) => {
//const digits = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 'a'];
const digits = ALL_CHARS.split("");
const roulette = svg
::append("g")
::attr("id", `digit-${id}`)
::style("filter", `url(#motionFilter-${id})`);
if(!noBlur)
roulette::style("filter", `url(#motionFilter-${id})`);

digits.forEach((el, i) => {
roulette
Expand Down Expand Up @@ -81,9 +94,15 @@ const setViewBox = (svg, width, height) => {
svg::attr("viewBox", `0 0 ${width} ${height}`);
svg::style("overflow", "hidden");
};

const pad = (str, length, char = ALL_CHARS[ALL_CHARS.length-1]) =>
str.padStart((str.length + length) / 2, char).padEnd(length, char);
const main = (initialOptions) => {
const {
//console.log(initialOptions);
if(initialOptions.continuousRun == true)
{
initialOptions.duration = 20000;
}
var {
el,
value,
initialValue = null,
Expand All @@ -92,7 +111,10 @@ const main = (initialOptions) => {
animationDelay = 100,
letterAnimationDelay = 100,
duration = 3000,
continuousRun = false,
noBlur = false,
} = initialOptions;

const element = select(el);
const computedStyle = window.getComputedStyle(element);
const fontSize = parseInt(computedStyle.fontSize, 10);
Expand All @@ -111,56 +133,79 @@ const main = (initialOptions) => {
createMask(defs, salt);

const prepareValues = (value, secondValue) => {
const values = String(value).replace(/ /g, "\u00a0").split("");

const values = String(value).replace(/ /g, ALL_CHARS[ALL_CHARS.length-1]).split("");

const digitIndex = String(value).search(/\d/);
//const digitIndex = String(value).search(/\d/);
while (secondValue.length > values.length) {
const char =
secondValue[secondValue.length - values.length - 1 + digitIndex];
values.splice(digitIndex, 0, isNaN(parseInt(char, 10)) ? char : "0");
secondValue[secondValue.length - values.length - 1];
values.splice(0, 0, char);
}
return values;
};

const initialString = String(initialValue || "0");
const values = prepareValues(String(value), initialString);
const initial = prepareValues(initialString, String(value));

let initValue = initialValue || "";
let Value = value;
if(initValue.length > Value.length)
{
Value = pad(Value, initValue.length," ");
}
else if(Value.length > initValue.length)
{
initValue = pad(initValue,Value.length, " ");
}
const initialString = String(initValue || "");
const values = prepareValues(String(Value), initialString);
const initial = prepareValues(initialString, String(Value));

const chars = values.map((char, i) => {

const id = `${i}-${salt}`;
if (isNaN(parseInt(char, 10)) || isNaN(parseInt(initial[i], 10))) {
if(!noBlur)
{
return {
isDigit: false,
node: createCharacter(svg, char, fontSize),
isDigit: true,
id: id,
node: createDigitRoulette(svg, fontSize, lineHeight, id, noBlur),
filter: createFilter(defs, id),
value: char,
offset: { x: 0, y: offset },
initial: initial[i],
offset: {
x: 0,
y: offset + Number(ALL_CHARS.indexOf(initial[i])) * (fontSize * lineHeight),
},
};
} else {
}
else {
return {
isDigit: true,
id: id,
node: createDigitRoulette(svg, fontSize, lineHeight, id),
filter: createFilter(defs, id),
value: Number(char),
initial: Number(initial[i]),
node: createDigitRoulette(svg, fontSize, lineHeight, id, noBlur),
//filter: createFilter(defs, id),
value: char,
initial: initial[i],
offset: {
x: 0,
y: offset + Number(initial[i]) * (fontSize * lineHeight),
y: offset + Number(ALL_CHARS.indexOf(initial[i])) * (fontSize * lineHeight),
},
};
}


});

console.log(JSON.stringify(chars));

const transitions = [];
const digits = chars.filter((char) => char.isDigit);
digits.forEach((digit, i) => {
const sourceDistance = digit.initial * (fontSize * lineHeight);
const sourceDistance = ALL_CHARS.indexOf(digit.initial) * (fontSize * lineHeight);
const targetDistance =
(ROTATIONS * DIGITS_COUNT + digit.value) * (fontSize * lineHeight);
(ROTATIONS * DIGITS_COUNT + ALL_CHARS.indexOf(digit.value)) * (fontSize * lineHeight);
const digitTransition = transition({
from: sourceDistance,
to: targetDistance,
duration: duration,
continuousRun: true,
delay: (digits.length - 1 - i) * letterAnimationDelay + animationDelay,
step(value) {
digit.offset.y =
Expand All @@ -176,7 +221,8 @@ const main = (initialOptions) => {
sourceDistance
) / 100
).toFixed(1);
digit.filter::attr("stdDeviation", `0 ${motionValue}`);
if(!noBlur)
digit.filter::attr("stdDeviation", `0 ${motionValue}`);
},
end:
i === 0
Expand Down Expand Up @@ -234,7 +280,22 @@ const main = (initialOptions) => {
main({ ...initialOptions, ...options });
};

return { cancel, pause, resume, restart };
const setTo = (toValue) => {
//duration = 3000;
//cancel();
restart({el,
value: toValue,
initialValue: pad("", 12),
//lineHeight = 1.35,
//letterSpacing = 1,
animationDelay: -3000,
letterAnimationDelay: 0,
duration: 4000,
continuousRun: false,})
transitions.forEach((transition) => transition.startFinish());
}

return { cancel, pause, resume, restart, setTo };
};

export default main;
20 changes: 19 additions & 1 deletion src/transition.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export default ({
to,
duration = 3000,
delay = 0,
continuousRun = false,
easing = cubicInOut,
start = (v) => v,
step = (v) => v,
Expand All @@ -16,6 +17,8 @@ export default ({
let paused = false;
let prevTime = 0;
let finished = false;
let finishNow = false;

const update = (timestamp) => {
if (finished) {
return;
Expand All @@ -25,6 +28,11 @@ export default ({
prevTime = timestamp;
start(value);
}
if(continuousRun && finishNow){

duration = timestamp + 3000;
continuousRun = false;
}

if (paused) {
startTime += timestamp - prevTime;
Expand All @@ -33,6 +41,8 @@ export default ({
const t =
Math.min(Math.max(timestamp - startTime - delay, 0), duration) / duration;
value = easing(t) * (to - from) + from;
//console.log("value in transition: " + value + " : " + to + " : " + from + " : " + t);
//console.log("duration: " + duration);
step(value);
if (t === 1) {
finished = true;
Expand All @@ -48,6 +58,14 @@ export default ({
const resume = () => {
paused = false;
};
const startFinish = () => {
//console.log("to before: " + to + " :duration: " + duration);
//to = toValue;
finishNow = true;
//startTime = 0;
//console.log("to after: " + to + " :duration: " + duration);

};

return { update, pause, resume };
return { update, pause, resume, startFinish };
};