Always fit rectangle
This commit is contained in:
parent
93c8e28c00
commit
9240dc4cb8
@ -142,13 +142,8 @@ function Surface (ronin) {
|
|||||||
if (isLine(shape)) {
|
if (isLine(shape)) {
|
||||||
this.context.drawImage(img, shape.a.x, shape.a.y, shape.b.x - shape.a.x, shape.b.y - shape.a.y)
|
this.context.drawImage(img, shape.a.x, shape.a.y, shape.b.x - shape.a.x, shape.b.y - shape.a.y)
|
||||||
} else if (isRect(shape)) {
|
} else if (isRect(shape)) {
|
||||||
if (img.width > img.height) {
|
const fit = fitRect({ w: img.width, h: img.height }, { w: shape.w, h: shape.h })
|
||||||
console.log('w bigger')
|
this.context.drawImage(img, shape.x, shape.y, fit.w, fit.h)
|
||||||
this.context.drawImage(img, shape.x, shape.y, shape.w, img.height * (shape.w / img.width))
|
|
||||||
} else {
|
|
||||||
console.log('h bigger')
|
|
||||||
this.context.drawImage(img, shape.x, shape.y, img.width * (shape.h / img.height), shape.h)
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
this.context.drawImage(img, shape.x, shape.y, img.width, img.height)
|
this.context.drawImage(img, shape.x, shape.y, img.width, img.height)
|
||||||
}
|
}
|
||||||
@ -280,4 +275,13 @@ function Surface (ronin) {
|
|||||||
function isLine (shape) {
|
function isLine (shape) {
|
||||||
return shape.a && !isNaN(shape.a.x) && !isNaN(shape.a.y) && shape.b && !isNaN(shape.b.x) && !isNaN(shape.b.y)
|
return shape.a && !isNaN(shape.a.x) && !isNaN(shape.a.y) && shape.b && !isNaN(shape.b.x) && !isNaN(shape.b.y)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function fitRect (image, container) {
|
||||||
|
image.ratio = image.w / image.h
|
||||||
|
container.ratio = container.w / container.h
|
||||||
|
return {
|
||||||
|
w: image.ratio < container.ratio ? container.h * image.ratio : container.w,
|
||||||
|
h: image.ratio > container.ratio ? container.w / image.ratio : container.h
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user