Last active
May 25, 2017 17:03
-
-
Save AshleyGrant/66ba3d3fabdf5e2d1c8d9f13f424fb7a to your computer and use it in GitHub Desktop.
Aurelia Gist
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <template> | |
| <require from="./my-box"></require> | |
| <svg width="550" height="550" | |
| mousedown.trigger="mouseDown($event)" | |
| mousemove.trigger="mouseMove($event)" | |
| mouseup.trigger="mouseUp($event)" | |
| > | |
| <my-box repeat.for="box of boxes" | |
| box.bind="box" | |
| selected.bind="box.id === currentId"> | |
| </my-box> | |
| </svg> | |
| <div> | |
| <input type="text" value.bind="msOffset" /> | |
| <button click.trigger="createBoxesArray()">Re-render</button> | |
| </div> | |
| </template> | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function getRandomInt(min, max) { | |
| return Math.floor(Math.random() * (max - min)) + min; | |
| } | |
| export class App { | |
| currentId = null; | |
| boxes = []; | |
| offsetX; | |
| offsetY; | |
| msOffset = 0; | |
| attached() { | |
| this.createBoxesArray(); | |
| } | |
| createBoxesArray() { | |
| setTimeout(() => { | |
| this.boxes = []; | |
| for (let i=0; i < 10000; i++) { | |
| const id = i; | |
| const x = getRandomInt(0, 500); | |
| const y = getRandomInt(0, 500); | |
| const box = { | |
| id, | |
| x, | |
| y | |
| }; | |
| if(this.msOffset > 0 ) { | |
| setTimeout( () => { | |
| this.boxes.push(box); | |
| }, this.msOffset); | |
| } else { | |
| this.boxes.push(box); | |
| } | |
| } | |
| },1); | |
| } | |
| mouseDown(event) { | |
| const id = Number(event.target.getAttribute("data-id")); | |
| const box = this.boxes[id]; | |
| const mouseX = event.clientX; | |
| const mouseY = event.clientY; | |
| this.offsetX = box.x - mouseX; | |
| this.offsetY = box.y - mouseY; | |
| this.currentId = id; | |
| } | |
| mouseMove(event) { | |
| event.preventDefault(); | |
| if (this.currentId !== null) { | |
| this.updateBox(this.currentId, event.clientX + this.offsetX, event.clientY + this.offsetY); | |
| } | |
| } | |
| mouseUp($event) { | |
| this.currentId = null; | |
| } | |
| updateBox(id, x, y) { | |
| const box = this.boxes[id]; | |
| box.x = x; | |
| box.y = y; | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <!doctype html> | |
| <html> | |
| <head> | |
| <title>Aurelia</title> | |
| <meta name="viewport" content="width=device-width, initial-scale=1"> | |
| </head> | |
| <body aurelia-app> | |
| <h1>Loading...</h1> | |
| <script src="https://jdanyow.github.io/rjs-bundle/node_modules/requirejs/require.js"></script> | |
| <script src="https://jdanyow.github.io/rjs-bundle/config.js"></script> | |
| <script src="https://jdanyow.github.io/rjs-bundle/bundles/aurelia.js"></script> | |
| <script src="https://jdanyow.github.io/rjs-bundle/bundles/babel.js"></script> | |
| <script> | |
| require(['aurelia-bootstrapper']); | |
| </script> | |
| </body> | |
| </html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <template> | |
| <svg> | |
| <rect | |
| data-id.bind="box.id" | |
| x.bind="box.x" | |
| y.bind="box.y" | |
| width="20" | |
| height="20" | |
| stroke="black" | |
| fill.bind="selected ? 'red' : 'transparent'" | |
| strokeWidth="1"></rect></svg> | |
| </template> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import {bindable, containerless} from 'aurelia-framework'; | |
| @containerless() | |
| export class MyBox { | |
| @bindable box; | |
| @bindable selected; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment