diff --git a/README.md b/README.md index 6613832..6aa938a 100644 --- a/README.md +++ b/README.md @@ -56,6 +56,7 @@ To save an image in memory, open an image file with Ronin, or drag an image file ## Library +- `(open ~name ~scale)` Imports a graphic file with format. - `(import ~name ~shape)` Imports a graphic file with format. - `(export ~name ~format ~quality)` Exports a graphic file with format. - `(pos ~x ~y)` Returns a position shape. diff --git a/desktop/sources/scripts/library.js b/desktop/sources/scripts/library.js index d7f412b..61ead0b 100644 --- a/desktop/sources/scripts/library.js +++ b/desktop/sources/scripts/library.js @@ -5,6 +5,14 @@ function Library (client) { // IO + + this.open = async (name, scale = 1) => { // Import a graphic and scale canvas to fit. + const img = client.cache.get(name) + const rect = this.rect(0, 0, img.width * scale, img.height * scale) + await this.resize(rect.w, rect.h).then(this.import(name, rect)) + return rect + } + this.import = async (name, shape, alpha = 1) => { // Imports a graphic file with format. const img = client.cache.get(name) if (!img) { client.log('No data for ' + name); return } @@ -71,19 +79,19 @@ function Library (client) { // Frame - this.resize = async (w = client.surface.bounds().w, h = client.surface.bounds().h, fit = true) => { // Resizes the canvas to target w and h, returns the rect. + this.resize = (w = client.surface.bounds().w, h = client.surface.bounds().h, fit = true) => { // Resizes the canvas to target w and h, returns the rect. if (w === this['get-frame']().w && h === this['get-frame']().h) { return } const rect = { x: 0, y: 0, w, h } const a = document.createElement('img') const b = document.createElement('img') a.src = client.surface.el.toDataURL() - await client.surface.resizeImage(a, b) + client.surface.resizeImage(a, b) client.surface.resize(rect, fit) return client.surface.draw(b, rect) } - this.rescale = async (w = 1, h = 1) => { // Rescales the canvas to target ratio of w and h, returns the rect. - const rect = { x: 0, y: 0, w: this['get-frame']().w * w, h: this['get-frame']().h * h } + this.rescale = async (w = 1, h) => { // Rescales the canvas to target ratio of w and h, returns the rect. + const rect = { x: 0, y: 0, w: this['get-frame']().w * w, h: this['get-frame']().h * (h || w) } const a = document.createElement('img') const b = document.createElement('img') a.src = client.surface.el.toDataURL()