From ef77a1b73d00f743a14fae06f4ada5e30c56cd19 Mon Sep 17 00:00:00 2001 From: Devine Lu Linvega Date: Sat, 13 Jul 2019 15:53:32 +0900 Subject: [PATCH] Draw image --- desktop/sources/scripts/library.js | 9 +++++++-- desktop/sources/scripts/surface.js | 10 ++++++++++ examples/load.lisp | 5 +++++ 3 files changed, 22 insertions(+), 2 deletions(-) create mode 100644 examples/load.lisp diff --git a/desktop/sources/scripts/library.js b/desktop/sources/scripts/library.js index f7ec9b4..da31614 100644 --- a/desktop/sources/scripts/library.js +++ b/desktop/sources/scripts/library.js @@ -2,8 +2,9 @@ function Library (ronin) { this.clear = (rect = this.select_all()) => { } - this.load = (path, rect) => { - console.log(path, rect) + this.draw = (path, rect) => { + ronin.surface.draw(path, rect) + return rect } // Rects @@ -28,6 +29,10 @@ function Library (ronin) { return { x: 0, y: 0, w: Math.floor(window.innerWidth / 2) - 15, h: Math.floor(window.innerHeight) - 30 } } + this.path = (path) => { + return path + } + // Copy/Paste this.clone = (a, b) => { diff --git a/desktop/sources/scripts/surface.js b/desktop/sources/scripts/surface.js index c593ed8..25c548a 100644 --- a/desktop/sources/scripts/surface.js +++ b/desktop/sources/scripts/surface.js @@ -79,6 +79,16 @@ function Surface (ronin) { this.context.closePath() } + this.draw = function (path, rect = this.getRect()) { + const img = new Image() + img.src = path + img.onload = () => { + const ratio = img.width / img.height + const scale = rect.w / img.width + this.context.drawImage(img, rect.x, rect.y, rect.w, img.height * scale) + } + } + this.clear = function (rect = this.getRect()) { this.context.clearRect(rect.x, rect.y, rect.w, rect.h) } diff --git a/examples/load.lisp b/examples/load.lisp new file mode 100644 index 0000000..3c30d92 --- /dev/null +++ b/examples/load.lisp @@ -0,0 +1,5 @@ +; load file + +((clear) + (stroke (draw ($path) ($rect)) 1 "red") +) \ No newline at end of file