Skip to content

Commit 7146dad

Browse files
author
Michał Skowronek
authored
Merge pull request #38 from bartpat/feature/initial-value
Feature/initial value
2 parents f3b8e87 + 0b83b46 commit 7146dad

File tree

4 files changed

+55
-9
lines changed

4 files changed

+55
-9
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ You can use it with other **options**:
1616
odoo.default({
1717
el: '.js-odoo',
1818
value: '£42,000,000',
19+
initialValue: '£900,000',
1920
lineHeight: 1.35,
2021
letterSpacing: 1,
2122
animationDelay: 100,

examples/initial.html

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<!doctype html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="utf-8">
5+
<title>Odoo</title>
6+
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
7+
<meta name="author" content="Coderitual">
8+
<meta name="description" content="Canvas 2d odometer effect">
9+
<meta name="viewport" content="width=device-width, minimum-scale=1.0, initial-scale=1.0, user-scalable=yes">
10+
<style type="text/css">
11+
@font-face{
12+
font-family: 'ITV Reem';
13+
src: url(assets/itvreem.woff) format('woff');
14+
}
15+
body {
16+
background-color: #999;
17+
font-family: 'ITV Reem';
18+
font-size: 82px;
19+
text-shadow: 1px 1px 5px rgba(0,0,0,0.5);
20+
fill: #fff;
21+
}
22+
</style>
23+
</head>
24+
<body>
25+
<div class="js-odoo"></div>
26+
<script src="/odoo.js"></script>
27+
<script>odoo.default({ el:'.js-odoo',value:'£42,000,000',initialValue:'£123,456' })</script>
28+
</body>
29+
</html>

lib/odoo.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/odoo.js

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ const setViewBox = (svg, width, height) => {
8686
export default ({
8787
el,
8888
value,
89+
initialValue = null,
8990
lineHeight = 1.35,
9091
letterSpacing = 1,
9192
animationDelay = 100,
@@ -109,13 +110,26 @@ export default ({
109110
createMask(defs, salt);
110111
createShadowFailFilter(defs);
111112

112-
const values = String(value)
113-
.replace(/ /g, '\u00a0')
114-
.split('');
113+
const prepareValues = (value, secondValue) => {
114+
const values = String(value)
115+
.replace(/ /g, '\u00a0')
116+
.split('');
117+
118+
const digitIndex = String(value).search(/\d/);
119+
while (secondValue.length > values.length) {
120+
const char = secondValue[secondValue.length - values.length - 1 + digitIndex];
121+
values.splice(digitIndex, 0, isNaN(parseInt(char, 10)) ? char : '0');
122+
}
123+
return values;
124+
};
125+
126+
const initialString = String(initialValue || '0');
127+
const values = prepareValues(String(value), initialString);
128+
const initial = prepareValues(initialString, String(value));
115129

116130
const chars = values.map((char, i) => {
117131
const id = `${i}-${salt}`;
118-
if(isNaN(parseInt(char, 10))) {
132+
if(isNaN(parseInt(char, 10)) || isNaN(parseInt(initial[i], 10))) {
119133
return {
120134
isDigit: false,
121135
node: createCharacter(svg, char, fontSize),
@@ -129,24 +143,26 @@ export default ({
129143
node: createDigitRoulette(svg, fontSize, lineHeight, id),
130144
filter: createFilter(defs, id),
131145
value: Number(char),
132-
offset: { x: 0, y: offset }
146+
initial: Number(initial[i]),
147+
offset: { x: 0, y: offset + Number(initial[i]) * (fontSize * lineHeight) }
133148
};
134149
}
135150
});
136151

137152
const transitions = [];
138153
const digits = chars.filter(char => char.isDigit);
139154
digits.forEach((digit, i) => {
155+
const sourceDistance = digit.initial * (fontSize * lineHeight);
140156
const targetDistance = (ROTATIONS * DIGITS_COUNT + digit.value) * (fontSize * lineHeight);
141157
const digitTransition = transition({
142-
from: 0,
158+
from: sourceDistance,
143159
to: targetDistance,
144160
delay: (digits.length - 1 - i) * letterAnimationDelay + animationDelay,
145161
step(value) {
146162
digit.offset.y = offset + value % ((fontSize * lineHeight) * DIGITS_COUNT);
147163
digit.node::attr('transform', `translate(${digit.offset.x}, ${digit.offset.y})`);
148-
const filterOrigin = targetDistance / 2;
149-
const motionValue = Math.abs(Math.abs(value - filterOrigin) - filterOrigin) / 100;
164+
const filterOrigin = (sourceDistance + targetDistance) / 2;
165+
const motionValue = (Math.abs(Math.abs(value - filterOrigin) - filterOrigin) - sourceDistance) / 100;
150166
digit.filter::attr('stdDeviation', `0 ${motionValue}`);
151167
},
152168
end: i === 0 ? () => cancelAnimation() : (e) => e

0 commit comments

Comments
 (0)