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 } 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 rect = { 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) await ronin.surface.resizeImage(a, b)
ronin.surface.resize(rect, true) console.log(b)
ronin.surface.resize(rect, fit)
return ronin.surface.draw(b, rect) 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) { 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 = 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 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) { this.resizeImage = function (src, dst, type = 'image/png', quality = 1.0) {
const tmp = new Image() return new Promise(resolve => {
let canvas const tmp = new Image()
let context let canvas
let cW = src.naturalWidth let context
let cH = src.naturalHeight let cW = src.naturalWidth
tmp.src = src.src let cH = src.naturalHeight
tmp.onload = function () { tmp.src = src.src
canvas = document.createElement('canvas') // resolve()
cW /= 2 tmp.onload = () => {
cH /= 2 canvas = document.createElement('canvas')
if (cW < src.width) { cW /= 2
cW = src.width 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))