Better logs

This commit is contained in:
Devine Lu Linvega 2019-07-16 10:25:40 +09:00
parent e9a84b376a
commit ed406ca1ed

View File

@ -116,7 +116,7 @@ function Surface (ronin) {
const img = new Image() const img = new Image()
img.src = path img.src = path
img.onload = () => { img.onload = () => {
console.log(`Open img: ${img.width}x${img.height}`) ronin.log(`Open ${img.width}x${img.height}`)
const rect = { x: 0, y: 0, w: img.width, h: img.height } const rect = { x: 0, y: 0, w: img.width, h: img.height }
this.resize(rect, true) this.resize(rect, true)
this.context.drawImage(img, 0, 0, img.width, img.height) this.context.drawImage(img, 0, 0, img.width, img.height)
@ -128,7 +128,7 @@ function Surface (ronin) {
this.draw = function (img, rect = this.getFrame(), callback = () => {}) { this.draw = function (img, rect = this.getFrame(), callback = () => {}) {
img.onload = () => { img.onload = () => {
console.log(`Drawing img: ${img.width}x${img.height}`) ronin.log(`Draw ${img.width}x${img.height}`)
this.context.drawImage(img, rect.x, rect.y, rect.w, rect.h) // no strect: img.height * (rect.w / img.width) this.context.drawImage(img, rect.x, rect.y, rect.w, rect.h) // no strect: img.height * (rect.w / img.width)
if (typeof callback === 'function') { if (typeof callback === 'function') {
callback() callback()
@ -137,7 +137,7 @@ function Surface (ronin) {
} }
this.crop = function (rect) { this.crop = function (rect) {
console.log(`Crop ${rect.w}x${rect.h} from ${rect.x}x${rect.y}`) ronin.log(`Crop ${rect.w}x${rect.h} from ${rect.x}x${rect.y}`)
const crop = this.getCrop(rect) const crop = this.getCrop(rect)
this.resize(rect, true) this.resize(rect, true)
this.context.drawImage(crop, 0, 0) this.context.drawImage(crop, 0, 0)
@ -200,38 +200,28 @@ function Surface (ronin) {
} }
this.resizeImage = function (src, dst, type = 'image/jpeg', quality = 0.92) { this.resizeImage = function (src, dst, type = 'image/jpeg', quality = 0.92) {
var tmp = new Image() const tmp = new Image()
var canvas let canvas
var context let context
var cW let cW = src.naturalWidth
var cH let cH = src.naturalHeight
cW = src.naturalWidth
cH = src.naturalHeight
tmp.src = src.src tmp.src = src.src
tmp.onload = function () { tmp.onload = function () {
canvas = document.createElement('canvas') canvas = document.createElement('canvas')
cW /= 2 cW /= 2
cH /= 2 cH /= 2
if (cW < src.width) { if (cW < src.width) {
cW = src.width cW = src.width
} }
if (cH < src.height) { if (cH < src.height) {
cH = src.height cH = src.height
} }
canvas.width = cW canvas.width = cW
canvas.height = cH canvas.height = cH
context = canvas.getContext('2d') context = canvas.getContext('2d')
context.drawImage(tmp, 0, 0, cW, cH) context.drawImage(tmp, 0, 0, cW, cH)
dst.src = canvas.toDataURL(type, quality) dst.src = canvas.toDataURL(type, quality)
if (cW <= src.width || cH <= src.height) { return } if (cW <= src.width || cH <= src.height) { return }
tmp.src = dst.src tmp.src = dst.src
} }
} }