Add transform, transform example.

This commit is contained in:
Aaron Morris 2019-07-21 20:15:56 -07:00
parent 605dd1fb9e
commit 1ca2f9776c
2 changed files with 64 additions and 1 deletions

View File

@ -17,6 +17,41 @@ function Library (ronin) {
return ronin.surface.open(path) return ronin.surface.open(path)
} }
// Transforms
this.move = (x, y) => {
ronin.surface.context.translate(x, y)
}
this.rotate = (angle) => {
ronin.surface.context.rotate(angle)
}
this.scale = (x, y) => {
ronin.surface.context.scale(x, y === undefined ? x : y)
}
this.transform = (a, b, c, d, e, f) => {
// a:hscale b:hskew c:vskew d:vscale e:hmove f:vmove
ronin.surface.context.transform(a, b, c, d, e, f)
}
this.setTransform = (a, b, c, d, e, f) => {
ronin.surface.context.setTransform(a, b, c, d, e, f)
}
this.resetTransform = () => {
ronin.surface.context.resetTransform()
}
this.pushTransform = () => {
ronin.surface.context.save()
}
this.popTransform = () => {
ronin.surface.context.restore()
}
// Shapes // Shapes
this.pos = (x, y, t = 'pos') => { // Returns a position shape. this.pos = (x, y, t = 'pos') => { // Returns a position shape.
@ -241,7 +276,10 @@ function Library (ronin) {
// Arrays // Arrays
this.map = async (fn, arr) => { this.map = async (fn, arr) => {
return Promise.all(arr.map(fn)) for (let i = 0; i < arr.length; i++) {
const arg = arr[i];
await fn(arg)
}
} }
this.filter = (fn, arr) => { this.filter = (fn, arr) => {

25
examples/transform.lisp Normal file
View File

@ -0,0 +1,25 @@
(resetTransform)
(clear)
(defn branch
(v)
(if
(gt v 0)
(
(scale 0.95)
(stroke
(line (pos 0 0) (pos 100 100))
10 "white")
(move 100 100)
(pushTransform)
(rotate (div v 50))
(branch (sub v 1))
(popTransform)
(pushTransform)
(rotate (div v -50))
(branch (sub v 1))
(popTransform)
)
)
)
(branch 10)