Skip to content

Instantly share code, notes, and snippets.

@AshleyGrant
Last active May 25, 2017 16:46
Show Gist options
  • Save AshleyGrant/8cff50b9312b448bf4807c43b6f4b29f to your computer and use it in GitHub Desktop.
Save AshleyGrant/8cff50b9312b448bf4807c43b6f4b29f to your computer and use it in GitHub Desktop.
Aurelia Gist
<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>
</template>
function getRandomInt(min, max) {
return Math.floor(Math.random() * (max - min)) + min;
}
export class App {
currentId = null;
boxes = [];
offsetX;
offsetY;
attached() {
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
};
this.boxes.push(box);
}
}
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;
}
}
<!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>
<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>
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