Last active
February 8, 2017 13:09
-
-
Save kkenan/70d4466c52b6ddf10df37ff6eecfcab6 to your computer and use it in GitHub Desktop.
New Twiddle
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 Ember from 'ember'; | |
const { computed } = Ember; | |
export default Ember.Component.extend({ | |
// Public Properties | |
width: 550, | |
height: 550, | |
boxCount: 10000, | |
boxWidth: 20, | |
boxHeight: 20, | |
// Ember Component Definition, | |
attributeBindings: ['width', 'height'], | |
tagName: 'svg', | |
// Actions | |
actions: { | |
onActivate: function (box) { | |
this.set('_activeBox', box); | |
}, | |
onDeactivate: function () { | |
this.set('_activeBox', null); | |
} | |
}, | |
mouseMove: function (ev) { | |
const activeBox = this.get('_activeBox'); | |
if (activeBox) { | |
const delta = this._getMouseDelta(ev.clientX, ev.clientY); | |
activeBox.set('x', activeBox.get('x') + delta.dx); | |
activeBox.set('y', activeBox.get('y') + delta.dy); | |
} | |
}, | |
mouseUp: function () { | |
this.set('_lastPositionX', undefined); | |
this.set('_lastPositionY', undefined); | |
}, | |
// Private Properties | |
_activeBox: null, | |
_boxes: computed('boxCount', function () { | |
const boxCount = this.get('boxCount'); | |
let result = []; | |
for (let i = 0; i < boxCount; i++) { | |
result.push(createObject(getRandomInt(0, 500), getRandomInt(0, 500))); | |
} | |
return result; | |
}), | |
_currentPosition: computed(function () { | |
return this.element.getBoundingClientRect(); | |
}), | |
_getMouseDelta: function (x, y) { | |
let lastX = this.getWithDefault('_lastPositionX', x); | |
let lastY = this.getWithDefault('_lastPositionY', y); | |
this.set('_lastPositionX', x); | |
this.set('_lastPositionY', y); | |
return { | |
dx: x - lastX, | |
dy: y - lastY | |
}; | |
} | |
}); | |
function createObject(x, y) { | |
return Ember.Object.create({ x, y }); | |
} | |
function getRandomInt(min, max) { | |
return Math.floor(Math.random() * (max - min)) + min; | |
} |
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 Ember from 'ember'; | |
const { computed } = Ember; | |
export default Ember.Component.extend({ | |
// Public Events | |
'on-activate': null, | |
'on-deactivate': null, | |
// Public Properties | |
box: null, | |
width: 20, | |
height: 20, | |
// Ember Component Definition, | |
tagName: 'g', | |
mouseDown: function(ev) { | |
this.set('_isActiveBox', true); | |
this.sendAction('on-activate', this.get('box')); | |
}, | |
mouseUp: function () { | |
this.set('_isActiveBox', false); | |
this.sendAction('on-deactivate'); | |
} | |
}); |
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 Ember from 'ember'; | |
export default Ember.Controller.extend({ | |
appName: 'Ember Twiddle' | |
}); |
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
{ | |
"version": "0.11.0", | |
"EmberENV": { | |
"FEATURES": {} | |
}, | |
"options": { | |
"use_pods": false, | |
"enable-testing": false | |
}, | |
"dependencies": { | |
"jquery": "https://cdnjs.cloudflare.com/ajax/libs/jquery/1.11.3/jquery.js", | |
"ember": "2.10.2", | |
"ember-data": "2.11.0", | |
"ember-template-compiler": "2.10.2", | |
"ember-testing": "2.10.2" | |
}, | |
"addons": {} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment