fix circles example

pull/1890/head
Rich Harris 7 years ago
parent c22b663b2a
commit 517f5443b4

@ -1,25 +1,26 @@
<!--
https://github.com/eugenkiss/7guis/wiki#circle-drawer
Click on the canvas to draw a circle. Click on a circle
to select it. Right-click on the canvas to adjust the
radius of the selected circle.
-->
<script> <script>
// [svelte-upgrade suggestion] let i = 0;
// manually refactor all references to __this let undoStack = [[]];
const __this = { let circles = [];
get: () => ({ i, undoStack, circles, selected, adjusting, adjusted }) let selected;
}; let adjusting = false;
let adjusted = false;
export let i = 0;
export let undoStack = [[]]; function handleClick(event) {
export let circles = []; if (adjusting) {
export let selected;
export let adjusting = false;
export let adjusted = false;
// [svelte-upgrade suggestion]
// review these functions and remove unnecessary 'export' keywords
export function handleClick(event) {
if (__this.get().adjusting) {
adjusting = false; adjusting = false;
// if nothing changed, rewind // if circle was adjusted,
if (__this.get().adjusted) push(); // push to the stack
if (adjusted) push();
return; return;
} }
@ -29,35 +30,34 @@
r: 50 r: 50
}; };
const circles = __this.get().circles; circles = circles.concat(circle);
circles.push(circle); selected = circle;
circles = circles, selected = circle;
push(); push();
} }
export function adjust(event) { function adjust(event) {
event.preventDefault(); selected.r = +event.target.value;
if (!__this.get().selected) return; circles = circles;
adjusting = true, adjusted = false; adjusted = true;
} }
export function select(circle, event) { function select(circle, event) {
if (!__this.get().adjusting) { if (!adjusting) {
event.stopPropagation(); event.stopPropagation();
selected = circle; selected = circle;
} }
} }
export function push() { function push() {
undoStack.splice(++i); const newUndoStack = undoStack.slice(0, ++i);
undoStack.push(clone(circles)); newUndoStack.push(clone(circles));
i = i, undoStack = undoStack; undoStack = newUndoStack;
} }
export function travel(d) { function travel(d) {
const circles = clone(undoStack[i += d]); circles = clone(undoStack[i += d]);
circles = circles, i = i, adjusting = false; adjusting = false;
} }
function clone(circles) { function clone(circles) {
@ -65,35 +65,6 @@
} }
</script> </script>
<!--
https://github.com/eugenkiss/7guis/wiki#circle-drawer
Click on the canvas to draw a circle. Click on a circle
to select it. Right-click on the canvas to adjust the
radius of the selected circle.
-->
<div class='controls'>
<button on:click='{() => travel(-1)}' disabled='{i === 0}'>undo</button>
<button on:click='{() => travel(+1)}' disabled='{i === undoStack.length -1}'>redo</button>
</div>
<svg on:click='{handleClick}' on:contextmenu='{adjust}'>
{#each circles as circle}
<circle cx='{circle.cx}' cy='{circle.cy}' r='{circle.r}'
on:click='{event => select(circle, event)}'
fill='{circle === selected ? "#ccc": "white"}'
/>
{/each}
</svg>
{#if adjusting}
<div class='adjuster'>
<p>adjust diameter of circle at {selected.cx}, {selected.cy}</p>
<input type='range' bind:value='selected.r' on:input='{() => adjusted = true}'>
</div>
{/if}
<style> <style>
.controls { .controls {
position: absolute; position: absolute;
@ -119,10 +90,38 @@ radius of the selected circle.
transform: translate(-50%,-50%); transform: translate(-50%,-50%);
padding: 1em; padding: 1em;
text-align: center; text-align: center;
background-color: rgba(255,255,255,0.5); background-color: rgba(255,255,255,0.7);
border-radius: 4px;
} }
input[type='range'] { input[type='range'] {
width: 100%; width: 100%;
} }
</style> </style>
<div class="controls">
<button on:click="{() => travel(-1)}" disabled="{i === 0}">undo</button>
<button on:click="{() => travel(+1)}" disabled="{i === undoStack.length -1}">redo</button>
i: {i}
</div>
<svg on:click={handleClick} >
{#each circles as circle}
<circle cx={circle.cx} cy={circle.cy} r={circle.r}
on:click="{event => select(circle, event)}"
on:contextmenu|stopPropagation|preventDefault="{() => {
adjusting = !adjusting;
if (adjusting) selected = circle;
}}"
fill="{circle === selected ? '#ccc': 'white'}"
/>
{/each}
</svg>
{#if adjusting}
<div class="adjuster">
<p>adjust diameter of circle at {selected.cx}, {selected.cy}</p>
<input type="range" value={selected.r} on:input={adjust}>
</div>
{/if}
Loading…
Cancel
Save