From 12c6422b0e4a7e196c007f9582740d6094a0af70 Mon Sep 17 00:00:00 2001 From: "yaoyao.xie" Date: Fri, 30 Nov 2018 16:55:10 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E5=96=84=E7=BB=98=E5=88=B6iamge?= =?UTF-8?q?=E7=9A=84aspectFill=E6=A8=A1=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/pen.js | 24 +++++++++++------------- 1 file changed, 11 insertions(+), 13 deletions(-) diff --git a/lib/pen.js b/lib/pen.js index 56c73c5..0d49efd 100644 --- a/lib/pen.js +++ b/lib/pen.js @@ -226,22 +226,20 @@ export default class Painter { height, } = this._preProcess(view); // 获得缩放到图片大小级别的裁减框 - let rWidth; - let rHeight; + let rWidth = view.sWidth; + let rHeight = view.sHeight; let startX = 0; let startY = 0; - if (view.sHeight > view.sWidth) { - rHeight = Math.round((view.sWidth / width) * height); - rWidth = view.sWidth; - } else { - rWidth = Math.round((view.sHeight / height) * width); - rHeight = view.sHeight; - } - if (view.sWidth > rWidth) { - startX = Math.round((view.sWidth - rWidth) / 2); - } - if (view.sHeight > rHeight) { + // 绘画区域比例 + const cp = width / height; + // 原图比例 + const op = view.sWidth / view.sHeight; + if (cp >= op) { + rHeight = rWidth / cp; startY = Math.round((view.sHeight - rHeight) / 2); + } else { + rWidth = rHeight * cp; + startX = Math.round((view.sWidth - rWidth) / 2); } if (view.css && view.css.mode === 'scaleToFill') { this.ctx.drawImage(view.url, -(width / 2), -(height / 2), width, height);