From a98e9a6b1ef7d270a496a3e78498477eca3d96aa Mon Sep 17 00:00:00 2001 From: Quentin Leonetti Date: Thu, 18 Jul 2019 11:51:25 +0200 Subject: [PATCH] fix async --- desktop/sources/scripts/library.js | 7 +++-- desktop/sources/scripts/lisp.js | 5 ++- desktop/sources/scripts/surface.js | 50 ++++++++++++++++-------------- examples/import.lisp | 5 +++ 4 files changed, 40 insertions(+), 27 deletions(-) create mode 100644 examples/import.lisp diff --git a/desktop/sources/scripts/library.js b/desktop/sources/scripts/library.js index 794af27..dd0ea70 100644 --- a/desktop/sources/scripts/library.js +++ b/desktop/sources/scripts/library.js @@ -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) } diff --git a/desktop/sources/scripts/lisp.js b/desktop/sources/scripts/lisp.js index ddc4b97..74c1067 100644 --- a/desktop/sources/scripts/lisp.js +++ b/desktop/sources/scripts/lisp.js @@ -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 } diff --git a/desktop/sources/scripts/surface.js b/desktop/sources/scripts/surface.js index 7945b97..6ee40ed 100644 --- a/desktop/sources/scripts/surface.js +++ b/desktop/sources/scripts/surface.js @@ -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 - } + }) } } diff --git a/examples/import.lisp b/examples/import.lisp new file mode 100644 index 0000000..36841c2 --- /dev/null +++ b/examples/import.lisp @@ -0,0 +1,5 @@ +( +(def a (import + "../static/crystal.jpg" + (rect 0 0 400 400))) +(echo a)) \ No newline at end of file