refactor for async
This commit is contained in:
parent
71dc7440f7
commit
acea1ad26d
@ -1,7 +1,6 @@
|
|||||||
function Library (ronin) {
|
function Library (ronin) {
|
||||||
this.open = (path, callback) => {
|
this.open = async (path) => {
|
||||||
ronin.surface.open(path, callback)
|
return ronin.surface.open(path)
|
||||||
return path
|
|
||||||
}
|
}
|
||||||
|
|
||||||
this.export = (path, type = 'image/png', quality = 1.0) => {
|
this.export = (path, type = 'image/png', quality = 1.0) => {
|
||||||
@ -12,27 +11,24 @@ function Library (ronin) {
|
|||||||
return path
|
return path
|
||||||
}
|
}
|
||||||
|
|
||||||
this.draw = (path, rect, callback) => {
|
this.draw = async (path, rect) => {
|
||||||
const img = new Image()
|
const img = new Image()
|
||||||
img.src = path
|
img.src = path
|
||||||
ronin.surface.draw(img, rect, callback)
|
return ronin.surface.draw(img, rect)
|
||||||
return rect
|
|
||||||
}
|
}
|
||||||
|
|
||||||
this.resize = (w = 1, h = 1, callback) => {
|
this.resize = async (w = 1, h = 1) => {
|
||||||
const rect = w <= 1 || h <= 1 ? { x: 0, y: 0, w: this.frame().w * w, h: this.frame().h * h } : { x: 0, y: 0, w, h }
|
const rect = w <= 1 || h <= 1 ? { x: 0, y: 0, w: this.frame().w * w, h: this.frame().h * h } : { x: 0, y: 0, w, h }
|
||||||
const a = document.createElement('img')
|
const a = document.createElement('img')
|
||||||
const b = document.createElement('img')
|
const b = document.createElement('img')
|
||||||
a.src = ronin.surface.el.toDataURL()
|
a.src = ronin.surface.el.toDataURL()
|
||||||
ronin.surface.resizeImage(a, b)
|
ronin.surface.resizeImage(a, b)
|
||||||
ronin.surface.resize(rect, true)
|
ronin.surface.resize(rect, true)
|
||||||
ronin.surface.draw(b, rect, callback)
|
return ronin.surface.draw(b, rect)
|
||||||
return rect
|
|
||||||
}
|
}
|
||||||
|
|
||||||
this.crop = (rect, callback) => {
|
this.crop = async (rect) => {
|
||||||
ronin.surface.crop(rect, callback)
|
return ronin.surface.crop(rect)
|
||||||
return rect
|
|
||||||
}
|
}
|
||||||
|
|
||||||
this.folder = (path = ronin.source.path) => {
|
this.folder = (path = ronin.source.path) => {
|
||||||
@ -77,13 +73,22 @@ function Library (ronin) {
|
|||||||
|
|
||||||
// Arrays
|
// Arrays
|
||||||
|
|
||||||
this.map = (fn, arr) => {
|
this.map = async (fn, arr) => {
|
||||||
return arr.map(fn)
|
return Promise.all(arr.map(fn))
|
||||||
}
|
}
|
||||||
|
|
||||||
this.filter = (fn, arr) => {
|
this._filter = (fn, arr) => {
|
||||||
return arr.filter(fn)
|
return arr.filter(fn)
|
||||||
}
|
}
|
||||||
|
this.filter= (fn, arr) => {
|
||||||
|
const list = Array.from(arr);
|
||||||
|
return Promise.all(list.map((element, index) => fn(element, index, list)))
|
||||||
|
.then(result => {
|
||||||
|
return list.filter((_, index) => {
|
||||||
|
return result[index];
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
this.reduce = (fn, arr, acc = 0) => {
|
this.reduce = (fn, arr, acc = 0) => {
|
||||||
return arr.reduce(fn, acc)
|
return arr.reduce(fn, acc)
|
||||||
|
@ -50,7 +50,7 @@ function Lisp (input, lib) {
|
|||||||
// docstring
|
// docstring
|
||||||
console.log(input[2].value)
|
console.log(input[2].value)
|
||||||
}
|
}
|
||||||
context.scope[identifier] = function () {
|
context.scope[identifier] = async function () {
|
||||||
const lambdaArguments = arguments
|
const lambdaArguments = arguments
|
||||||
const lambdaScope = argumentNames.reduce(function (acc, x, i) {
|
const lambdaScope = argumentNames.reduce(function (acc, x, i) {
|
||||||
acc[x.value] = lambdaArguments[i]
|
acc[x.value] = lambdaArguments[i]
|
||||||
@ -60,7 +60,7 @@ function Lisp (input, lib) {
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
lambda: function (input, context) {
|
lambda: function (input, context) {
|
||||||
return function () {
|
return async function () {
|
||||||
const lambdaArguments = arguments
|
const lambdaArguments = arguments
|
||||||
const lambdaScope = input[1].reduce(function (acc, x, i) {
|
const lambdaScope = input[1].reduce(function (acc, x, i) {
|
||||||
acc[x.value] = lambdaArguments[i]
|
acc[x.value] = lambdaArguments[i]
|
||||||
@ -69,23 +69,23 @@ function Lisp (input, lib) {
|
|||||||
return interpret(input[2], new Context(lambdaScope, context))
|
return interpret(input[2], new Context(lambdaScope, context))
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
if: function (input, context) {
|
if: async function (input, context) {
|
||||||
if (interpret(input[1], context)) {
|
if (await interpret(input[1], context)) {
|
||||||
return interpret(input[2], context)
|
return interpret(input[2], context)
|
||||||
}
|
}
|
||||||
return input[3] ? interpret(input[3], context) : []
|
return input[3] ? interpret(input[3], context) : []
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const interpretList = function (input, context) {
|
const interpretList = async function (input, context) {
|
||||||
if (input.length > 0 && input[0].value in special) {
|
if (input.length > 0 && input[0].value in special) {
|
||||||
return special[input[0].value](input, context)
|
return special[input[0].value](input, context)
|
||||||
}
|
}
|
||||||
const list = input.map(function (x) { return interpret(x, context) })
|
const list = await Promise.all(input.map(function (x) { return interpret(x, context) }))
|
||||||
return list[0] instanceof Function ? list[0].apply(undefined, list.slice(1)) : list
|
return list[0] instanceof Function ? list[0].apply(undefined, list.slice(1)) : list
|
||||||
}
|
}
|
||||||
|
|
||||||
const interpret = function (input, context) {
|
const interpret = async function (input, context) {
|
||||||
if (!input) { console.warn('error', context.scope); return null }
|
if (!input) { console.warn('error', context.scope); return null }
|
||||||
|
|
||||||
if (context === undefined) {
|
if (context === undefined) {
|
||||||
@ -134,7 +134,7 @@ function Lisp (input, lib) {
|
|||||||
return parenthesize(tokenize(input))
|
return parenthesize(tokenize(input))
|
||||||
}
|
}
|
||||||
|
|
||||||
this.toPixels = function () {
|
this.toPixels = async function () {
|
||||||
return interpret(this.parse(input))
|
return interpret(this.parse(input))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -106,32 +106,32 @@ function Surface (ronin) {
|
|||||||
|
|
||||||
// IO
|
// IO
|
||||||
|
|
||||||
this.open = function (path, callback = () => {}) {
|
this.open = function (path) {
|
||||||
const img = new Image()
|
return new Promise(resolve => {
|
||||||
img.src = path
|
const img = new Image()
|
||||||
img.onload = () => {
|
img.src = path
|
||||||
ronin.log(`Open ${img.width}x${img.height}`)
|
img.onload = () => {
|
||||||
const rect = { x: 0, y: 0, w: img.width, h: img.height }
|
//ronin.log(`Open ${img.width}x${img.height}`)
|
||||||
this.resize(rect, true)
|
const rect = { x: 0, y: 0, w: img.width, h: img.height }
|
||||||
this.context.drawImage(img, 0, 0, img.width, img.height)
|
this.resize(rect, true)
|
||||||
if (typeof callback === 'function') {
|
this.context.drawImage(img, 0, 0, img.width, img.height)
|
||||||
callback()
|
resolve()
|
||||||
}
|
}
|
||||||
}
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
this.draw = function (img, rect = this.getFrame(), callback = () => {}) {
|
this.draw = function (img, rect = this.getFrame()) {
|
||||||
img.onload = () => {
|
return new Promise(resolve => {
|
||||||
ronin.log(`Draw ${img.width}x${img.height}`)
|
img.onload = () => {
|
||||||
|
//ronin.log(`Draw ${img.width}x${img.height}`)
|
||||||
this.context.drawImage(img, rect.x, rect.y, rect.w, rect.h) // no strect: img.height * (rect.w / img.width)
|
this.context.drawImage(img, rect.x, rect.y, rect.w, rect.h) // no strect: img.height * (rect.w / img.width)
|
||||||
if (typeof callback === 'function') {
|
resolve()
|
||||||
callback()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
this.crop = function (rect) {
|
this.crop = function (rect) {
|
||||||
ronin.log(`Crop ${rect.w}x${rect.h} from ${rect.x}x${rect.y}`)
|
// ronin.log(`Crop ${rect.w}x${rect.h} from ${rect.x}x${rect.y}`)
|
||||||
const crop = this.getCrop(rect)
|
const crop = this.getCrop(rect)
|
||||||
this.resize(rect, true)
|
this.resize(rect, true)
|
||||||
this.context.drawImage(crop, 0, 0)
|
this.context.drawImage(crop, 0, 0)
|
||||||
|
@ -2,15 +2,6 @@
|
|||||||
|
|
||||||
(
|
(
|
||||||
(clear)
|
(clear)
|
||||||
|
(open "../static/crystal.jpg")
|
||||||
; Filter
|
(crop (rect 100 100 400 400))
|
||||||
|
|
||||||
(def filter-action
|
|
||||||
(lambda () (crop (rect 100 100 400 400))))
|
|
||||||
|
|
||||||
; Draw photo
|
|
||||||
|
|
||||||
(open
|
|
||||||
"../../PREVIEW.jpg"
|
|
||||||
filter-action)
|
|
||||||
)
|
)
|
@ -3,7 +3,7 @@
|
|||||||
(
|
(
|
||||||
(clear)
|
(clear)
|
||||||
|
|
||||||
; Filter
|
; Glitch
|
||||||
|
|
||||||
(defn glitch
|
(defn glitch
|
||||||
(rec)
|
(rec)
|
||||||
@ -19,6 +19,7 @@
|
|||||||
|
|
||||||
(draw
|
(draw
|
||||||
"../static/crystal.jpg"
|
"../static/crystal.jpg"
|
||||||
(rect 0 0 400 400)
|
(rect 0 0 400 400))
|
||||||
(lambda () (glitch 2000)))
|
|
||||||
|
(glitch 500)
|
||||||
)
|
)
|
@ -1,13 +1,9 @@
|
|||||||
; scale file
|
; scale file
|
||||||
(
|
(
|
||||||
; Filter
|
|
||||||
|
|
||||||
(def filter-action
|
(open "../static/crystal.jpg")
|
||||||
(lambda () (pixels
|
(pixels
|
||||||
(frame)
|
(frame)
|
||||||
saturation
|
saturation
|
||||||
0.5)
|
12)
|
||||||
))
|
|
||||||
|
|
||||||
(open (path "/Users/VillaMoirai/Desktop/clip.jpg") filter-action)
|
|
||||||
)
|
)
|
@ -2,20 +2,11 @@
|
|||||||
|
|
||||||
(
|
(
|
||||||
(clear)
|
(clear)
|
||||||
|
|
||||||
; Filter
|
|
||||||
|
|
||||||
(def filter-action
|
|
||||||
(lambda () (pixels
|
|
||||||
(rect 0 0 500 500)
|
|
||||||
saturation
|
|
||||||
0.5)
|
|
||||||
))
|
|
||||||
|
|
||||||
; Draw photo
|
|
||||||
|
|
||||||
(draw
|
(draw
|
||||||
"../../PREVIEW.jpg"
|
"../../PREVIEW.jpg"
|
||||||
(frame)
|
(frame))
|
||||||
filter-action)
|
(pixels
|
||||||
|
(rect 0 0 500 500)
|
||||||
|
saturation
|
||||||
|
0.5)
|
||||||
)
|
)
|
@ -6,15 +6,16 @@
|
|||||||
(defn place
|
(defn place
|
||||||
(rec)
|
(rec)
|
||||||
(if (gt rec 0)
|
(if (gt rec 0)
|
||||||
((draw "../static/crystal.jpg"
|
(
|
||||||
(rect
|
(draw "../static/crystal.jpg"
|
||||||
(random 200)
|
(rect
|
||||||
(random 200)
|
(random 200)
|
||||||
(random 200)
|
(random 200)
|
||||||
(random 200))
|
(random 200)
|
||||||
(lambda () (place (sub rec 1)))
|
(random 200)))
|
||||||
)))
|
(place (sub rec 1))
|
||||||
)
|
))
|
||||||
|
)
|
||||||
|
|
||||||
(place 30)
|
(place 30)
|
||||||
)
|
)
|
@ -2,15 +2,6 @@
|
|||||||
|
|
||||||
(
|
(
|
||||||
(clear)
|
(clear)
|
||||||
|
(open "../../PREVIEW.jpg")
|
||||||
; Filter
|
(resize 0.5 0.5)
|
||||||
|
|
||||||
(def filter-action
|
|
||||||
(lambda () (resize 0.5 0.5)))
|
|
||||||
|
|
||||||
; Draw photo
|
|
||||||
|
|
||||||
(open
|
|
||||||
"../../PREVIEW.jpg"
|
|
||||||
filter-action)
|
|
||||||
)
|
)
|
Loading…
x
Reference in New Issue
Block a user