Added open command

This commit is contained in:
neauoire 2020-03-11 16:05:05 +09:00
parent dc60122de6
commit ca69d99a9c
2 changed files with 13 additions and 4 deletions

View File

@ -56,6 +56,7 @@ To save an image in memory, open an image file with Ronin, or drag an image file
## Library ## Library
- `(open ~name ~scale)` Imports a graphic file with format.
- `(import ~name ~shape)` 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. - `(export ~name ~format ~quality)` Exports a graphic file with format.
- `(pos ~x ~y)` Returns a position shape. - `(pos ~x ~y)` Returns a position shape.

View File

@ -5,6 +5,14 @@
function Library (client) { function Library (client) {
// IO // 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. this.import = async (name, shape, alpha = 1) => { // Imports a graphic file with format.
const img = client.cache.get(name) const img = client.cache.get(name)
if (!img) { client.log('No data for ' + name); return } if (!img) { client.log('No data for ' + name); return }
@ -71,19 +79,19 @@ function Library (client) {
// Frame // 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 } if (w === this['get-frame']().w && h === this['get-frame']().h) { return }
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 = client.surface.el.toDataURL() a.src = client.surface.el.toDataURL()
await client.surface.resizeImage(a, b) client.surface.resizeImage(a, b)
client.surface.resize(rect, fit) client.surface.resize(rect, fit)
return client.surface.draw(b, rect) 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. 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 } 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 a = document.createElement('img')
const b = document.createElement('img') const b = document.createElement('img')
a.src = client.surface.el.toDataURL() a.src = client.surface.el.toDataURL()