Created
April 25, 2016 08:44
-
-
Save rocbear/c8f4e98dbe2b7c816ea33beb844e9f1e to your computer and use it in GitHub Desktop.
Immutable Canvas helper objects. No 'class' syntax, immutability through Object.assign and values scaled to devicePixelRatio
This file contains 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
let ScaledValue = { | |
actual: 0, | |
scaled() { | |
return this.actual * devicePixelRatio | |
}, | |
create(value) { | |
return Object.assign({}, this, { actual: value }) | |
} | |
} | |
let Point = { | |
values: [], | |
scaled() { | |
return this.values.map(v => v.scaled()) | |
}, | |
create(x, y) { | |
let xs = ScaledValue.create(x) | |
let xy = ScaledValue.create(y) | |
return Object.assign({}, this, { values: [xs, xy] }) | |
} | |
} | |
let p1 = Point.create(10, 10) | |
console.log(p1.scaled()) // [20, 20] (where devicePixelRatio = 2) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment