Skip to content

Instantly share code, notes, and snippets.

@rocbear
Created April 25, 2016 08:44
Show Gist options
  • Save rocbear/c8f4e98dbe2b7c816ea33beb844e9f1e to your computer and use it in GitHub Desktop.
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
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