[docs] Fix the speed of the typewriter example to scale correctly (#6568)

pull/6695/head
duarte-pompeu 3 years ago committed by GitHub
parent 3c5cea114c
commit 8d3f4512f6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -963,20 +963,22 @@ A custom transition function can also return a `tick` function, which is called
<script>
export let visible = false;
function typewriter(node, { speed = 50 }) {
function typewriter(node, { speed = 1 }) {
const valid = (
node.childNodes.length === 1 &&
node.childNodes[0].nodeType === Node.TEXT_NODE
);
if (!valid) return {};
if (!valid) {
throw new Error(`This transition only works on elements with a single text node child`);
}
const text = node.textContent;
const duration = text.length * speed;
const duration = text.length / (speed * 0.01);
return {
duration,
tick: (t, u) => {
tick: t => {
const i = ~~(text.length * t);
node.textContent = text.slice(0, i);
}
@ -985,7 +987,7 @@ A custom transition function can also return a `tick` function, which is called
</script>
{#if visible}
<p in:typewriter="{{ speed: 20 }}">
<p in:typewriter="{{ speed: 1 }}">
The quick brown fox jumps over the lazy dog
</p>
{/if}

@ -1,7 +1,7 @@
<script>
let visible = false;
function typewriter(node, { speed = 50 }) {
function typewriter(node, { speed = 1 }) {
const valid = (
node.childNodes.length === 1 &&
node.childNodes[0].nodeType === Node.TEXT_NODE
@ -12,7 +12,7 @@
}
const text = node.textContent;
const duration = text.length * speed;
const duration = text.length / (speed * 0.01);
return {
duration,
@ -30,7 +30,7 @@
</label>
{#if visible}
<p in:typewriter>
<p in:typewriter out:typewriter>
The quick brown fox jumps over the lazy dog
</p>
{/if}

@ -1,7 +1,7 @@
<script>
let visible = false;
function typewriter(node, { speed = 50 }) {
function typewriter(node, { speed = 1 }) {
const valid = (
node.childNodes.length === 1 &&
node.childNodes[0].nodeType === Node.TEXT_NODE
@ -12,7 +12,7 @@
}
const text = node.textContent;
const duration = text.length * speed;
const duration = text.length / (speed * 0.01);
return {
duration,
@ -30,7 +30,7 @@
</label>
{#if visible}
<p in:typewriter>
<p in:typewriter out:typewriter>
The quick brown fox jumps over the lazy dog
</p>
{/if}

@ -5,7 +5,7 @@ title: Custom JS transitions
While you should generally use CSS for transitions as much as possible, there are some effects that can't be achieved without JavaScript, such as a typewriter effect:
```js
function typewriter(node, { speed = 50 }) {
function typewriter(node, { speed = 1 }) {
const valid = (
node.childNodes.length === 1 &&
node.childNodes[0].nodeType === Node.TEXT_NODE
@ -16,7 +16,7 @@ function typewriter(node, { speed = 50 }) {
}
const text = node.textContent;
const duration = text.length * speed;
const duration = text.length / (speed * 0.01);
return {
duration,

Loading…
Cancel
Save