From 46bf50f663be720f524706ac4c9a9297367b009c Mon Sep 17 00:00:00 2001 From: Devine Lu Linvega Date: Sat, 3 Aug 2019 12:39:07 +0900 Subject: [PATCH] Added open with orientation --- desktop/sources/scripts/commander.js | 2 +- desktop/sources/scripts/library.js | 4 ++-- desktop/sources/scripts/surface.js | 25 +++++++++++++++++++++---- 3 files changed, 24 insertions(+), 7 deletions(-) diff --git a/desktop/sources/scripts/commander.js b/desktop/sources/scripts/commander.js index 9f2867d..daee2ef 100644 --- a/desktop/sources/scripts/commander.js +++ b/desktop/sources/scripts/commander.js @@ -268,7 +268,7 @@ function Commander (ronin) { // Splash this.splash = `; welcome to ronin -; v2.28 +; v2.29 (clear) (def logo-path "M60,60 L195,60 A45,45 0 0,1 240,105 A45,45 0 0,1 195,150 L60,150 M195,150 A45,45 0 0,1 240,195 L240,240 ") (stroke diff --git a/desktop/sources/scripts/library.js b/desktop/sources/scripts/library.js index 2c0f25b..7b6533b 100644 --- a/desktop/sources/scripts/library.js +++ b/desktop/sources/scripts/library.js @@ -19,8 +19,8 @@ function Library (ronin) { return path } - this.open = async (path, ratio = 1) => { // Imports a graphic file and resizes the frame. - return ronin.surface.open(path, ratio) + this.open = async (path, ratio = 1, orientation = 0, mirrorx = 1, mirrory = 1) => { // Imports a graphic file and resizes the frame. + return ronin.surface.open(path, ratio, orientation, mirrorx, mirrory) } this.exit = (force = false) => { // Exits Ronin. diff --git a/desktop/sources/scripts/surface.js b/desktop/sources/scripts/surface.js index c490093..b7b8ab0 100644 --- a/desktop/sources/scripts/surface.js +++ b/desktop/sources/scripts/surface.js @@ -149,19 +149,36 @@ function Surface (ronin) { // IO - this.open = function (path, ratio = 1) { + this.open = function (path, ratio = 1, orientation = 0, mirrorx = 1, mirrory = 1) { return new Promise(resolve => { const img = new Image() img.src = path img.onload = () => { - const rect = { x: 0, y: 0, w: parseInt(img.width * ratio), h: parseInt(img.height * ratio) } - this.resize(rect, true) - this.context.drawImage(img, 0, 0, rect.w, rect.h) + this.drawWithOrientation(img, ratio, orientation, mirrorx, mirrory) resolve() } }) } + this.drawWithOrientation = function (img, ratio, orientation = 0, mirrorx = 1, mirrory = 1) { // x, y, w, h, degrees + const outputRect = { x: 0, y: 0, w: parseInt((orientation === 1 || orientation === 3 ? img.height : img.width) * ratio), h: parseInt((orientation === 1 || orientation === 3 ? img.width : img.height) * ratio) } + const rect = { x: 0, y: 0, w: parseInt(img.width * ratio), h: parseInt(img.height * ratio) } + this.resize(outputRect, true) + this.context.save() + + this.context.rotate((orientation * 90) * Math.PI / 180.0) + if (orientation === 1) { + this.context.translate(0, -rect.h) + } else if (orientation === 2) { + this.context.translate(-rect.w, -rect.h) + } else if (orientation === 3) { + this.context.translate(-rect.w, 0) + } + this.context.scale(mirrorx, mirrorx) + this.context.drawImage(img, rect.x, rect.y, rect.w * mirrorx, rect.h * mirrory) + this.context.restore() + } + this.draw = function (img, shape = this.getFrame(), alpha = 1) { return new Promise(resolve => { img.onload = () => {