fix async

This commit is contained in:
Quentin Leonetti 2019-07-18 11:51:25 +02:00
parent 533c4d3f27
commit a98e9a6b1e
4 changed files with 40 additions and 27 deletions

View File

@ -238,13 +238,14 @@ function Library (ronin) {
return { x: rect.x, y: rect.y, w: rect.w * w, h: rect.h * h }
}
this.resize = async (w, h) => { // Resizes the canvas to target w and h, returns the rect.
this.resize = async (w, h, fit = true) => { // Resizes the canvas to target w and h, returns the rect.
const rect = { x: 0, y: 0, w, h }
const a = document.createElement('img')
const b = document.createElement('img')
a.src = ronin.surface.el.toDataURL()
ronin.surface.resizeImage(a, b)
ronin.surface.resize(rect, true)
await ronin.surface.resizeImage(a, b)
console.log(b)
ronin.surface.resize(rect, fit)
return ronin.surface.draw(b, rect)
}

View File

@ -72,7 +72,10 @@ function Lisp (input, lib) {
if (input.length > 0 && input[0].value in special) {
return special[input[0].value](input, context)
}
const list = await Promise.all(input.map(function (x) { return interpret(x, context) }))
const list = []
for(let i = 0; i < input.length; i++) {
list.push(await interpret(input[i], context))
}
return list[0] instanceof Function ? list[0].apply(undefined, list.slice(1)) : list
}

View File

@ -206,29 +206,33 @@ function Surface (ronin) {
}
this.resizeImage = function (src, dst, type = 'image/png', quality = 1.0) {
const tmp = new Image()
let canvas
let context
let cW = src.naturalWidth
let cH = src.naturalHeight
tmp.src = src.src
tmp.onload = function () {
canvas = document.createElement('canvas')
cW /= 2
cH /= 2
if (cW < src.width) {
cW = src.width
return new Promise(resolve => {
const tmp = new Image()
let canvas
let context
let cW = src.naturalWidth
let cH = src.naturalHeight
tmp.src = src.src
// resolve()
tmp.onload = () => {
canvas = document.createElement('canvas')
cW /= 2
cH /= 2
if (cW < src.width) {
cW = src.width
}
if (cH < src.height) {
cH = src.height
}
canvas.width = cW
canvas.height = cH
context = canvas.getContext('2d')
context.drawImage(tmp, 0, 0, cW, cH)
dst.src = canvas.toDataURL(type, quality)
if (cW <= src.width || cH <= src.height) { return resolve()}
tmp.src = dst.src
return resolve()
}
if (cH < src.height) {
cH = src.height
}
canvas.width = cW
canvas.height = cH
context = canvas.getContext('2d')
context.drawImage(tmp, 0, 0, cW, cH)
dst.src = canvas.toDataURL(type, quality)
if (cW <= src.width || cH <= src.height) { return }
tmp.src = dst.src
}
})
}
}

5
examples/import.lisp Normal file
View File

@ -0,0 +1,5 @@
(
(def a (import
"../static/crystal.jpg"
(rect 0 0 400 400)))
(echo a))