http://imgur.com/3Zl6oRA | ๊ทธ๋ฆผ 1
http://imgur.com/Wr006qF | ๊ทธ๋ฆผ 2
๊ทธ๋ฆผ 1์ ์บก์ฒํ๋ ค๋ div์ด๋ฉฐ ์ด๋ฏธ์ง๋ฅผ ์บก์ฒํ ํ ํฌ๊ธฐ๊ฐ ๋ณ๊ฒฝ๋ฉ๋๋ค. *๊ทธ๋ฆผ 2.
๋๊ตฌ๋ ์ง ์ด๊ฒ์ ๊ณ ์น ์ ์์ต๋๊น?
์๋ ํ์ธ์, ์ ๋ ์ด๊ฒ์ด ํ์ํ๊ณ ์ ํ๋ก์ ํธ์์ ์๋ํ๋๋ก ํดํนํ์์ ์๋ ค๋๋ฆฝ๋๋ค. ์๋์์ ์ ์๊ฒ ๋ง๋ ์ฝ๋๋ฅผ ์ ๊ณตํฉ๋๋ค. ๊ทธ๋ฌ๋ ์ด๊ฒ์ ํดํน์ ๋๋ค. ์ ๋ ์ด๊ฒ์ ๋ณด๋ค ์ ์ ํ ๋ฐฉ์์ผ๋ก ํ๋ก์ ํธ์ ํตํฉํ ์ง์์ด ์์ต๋๋ค.
```
ํค: '๊ทธ๋ฆฌ๊ธฐ ์ด๋ฏธ์ง',
๊ฐ: function drawImage(์ด๋ฏธ์ง, ์์ค, ๋์) {
/ ์ฌ์ฉ์ ์ ์ ์ฝ๋ ์์ /
var newWidth = 30;
var newHeight = 30;
var newX = destination.left;
var newY = destination.top;
// console.log(image, source, destination);
if (source.width/destination.width > source.height/destination.height) {
newWidth = destination.width;
newHeight = source.height * (destination.width / source.width);
newY = destination.top + (destination.height - newHeight) / 2;
} else {
newWidth = source.width * (destination.height / source.height);
newHeight = destination.height;
newX = destination.left + (destination.width - newWidth) / 2;
}
// console.log(newWidth, newHeight);
this.ctx.drawImage(image, source.left, source.top, source.width, source.height,
newX, newY,
newWidth, newHeight);
// destination.width,
// destination.height * (source.height / source.width)
// destination.width, destination.height);
/**END CUSTOM CODE**/
}
```
LanderBeeuwsaert์ ์๋ฃจ์ ์ ์ฌ์ฉํ์ฌ(html2canvas.js๋ฅผ ๋ค์ด๋ก๋ํ์ฌ ์ฝ๋๋ฅผ ํธ์งํ ์ ์๋๋ก ํ์ฌ)
if (source.width/destination.width > source.height/destination.height) {
~์ ํจ๊ป
if (source.width/destination.width < source.height/destination.height) {
์ํ๋ ๊ฒฐ๊ณผ๋ฅผ ์ป์ ์ ์์ต๋๋ค. ๊ทธ๊ฒ์ ์ ์ด๋ ๋๋ฅผ ์ํด ์ผํ์ต๋๋ค.
์ด์ ๋ํ ์ ๋ฐ์ดํธ๊ฐ ์์ต๋๊น?
@LanderBeeuwsaert ์๋ฒฝํ๊ฒ ์๋ํด ์ฃผ์ ์ ๊ฐ์ฌํฉ๋๋ค. ๋ง์คํฐ์๊ฒ ์ถ๊ฐํด์ผ ํฉ๋๋ค. ์์ฃผ ์ข์ ๊ธฐ์ฌ์ ๋๋ค.
@niklasvh ํ๋ก์ ํธ๋ฅผ ์
๋ฐ์ดํธํ ๊ฒ ๊ฐ์ต๋๋ค.
์ด ์ฝ๋๋ฅผ ์
๋ ฅํ๊ณ ์ถ์ต๋๋ค. ์ด๋ณด๋ค ๋ ์ ํํ ์ฝ๋๊ฐ ํ์ํ ๊ฒฝ์ฐ ์๋ ค์ฃผ์ญ์์ค.
@LanderBeeuwsaert ์ด ํดํน์ ์ด๋์์ ๊ตฌํํด์ผ ํ ๊น์?
@SamuraiPetrus
๋ค, ํ๋ก์ ํธ ๊ตฌ์กฐ๊ฐ 0.5์์ 1.0.0์ผ๋ก ํ์ดํ์คํฌ๋ฆฝํธ๋ก ๋ณํ๋๋ฉด์ ์์ ํ ๋ฐ๋์์ต๋๋ค.
1.0.0์์ ์ด๋๋ก ๊ฐ์ผํ๋์ง ์์ ์ ๊ฒ์ํด์ PR์ ํ ์ ์์๋๋ฐ ์ฐพ์ง ๋ชปํ์ต๋๋ค. ๊ทธ๋์ ์ ๋ ์ด ํดํน์ผ๋ก ์ฌ์ ํ 0.5๋ฅผ ์ฌ์ฉํ๊ณ ์์ต๋๋ค.
1.0.0-alpha.9
๋ฒ์ ์ ์ฌ์ฉํ๊ณ patch-package
์ ๊ฐ์ @LanderBeeuwsaert ์ฝ๋์ ํจ๊ป html2canvas+1.0.0-alpha.9.patch
.
diff --git a/node_modules/html2canvas/dist/html2canvas.js b/node_modules/html2canvas/dist/html2canvas.js
index 5f34081..66d1909 100644
--- a/node_modules/html2canvas/dist/html2canvas.js
+++ b/node_modules/html2canvas/dist/html2canvas.js
@@ -2443,7 +2443,22 @@ var CanvasRenderer = function () {
}, {
key: 'drawImage',
value: function drawImage(image, source, destination) {
- this.ctx.drawImage(image, source.left, source.top, source.width, source.height, destination.left, destination.top, destination.width, destination.height);
+ var newWidth = 30;
+ var newHeight = 30;
+ var newX = destination.left;
+ var newY = destination.top;
+
+ if (source.width/destination.width < source.height/destination.height) {
+ newWidth = destination.width;
+ newHeight = source.height * (destination.width / source.width);
+ newY = destination.top + (destination.height - newHeight) / 2;
+ } else {
+ newWidth = source.width * (destination.height / source.height);
+ newHeight = destination.height;
+ newX = destination.left + (destination.width - newWidth) / 2;
+ }
+
+ this.ctx.drawImage(image, source.left, source.top, source.width, source.height, newX, newY, newWidth, newHeight);
}
}, {
key: 'drawShape',
์ ์ฒด ๊ฒฝ๋ก๋ฅผ ์ ๊ณตํ์ฌ ์ถ์๋์ง ์์ ๋ฒ์ ์ ๊ฐ์ ธ์ค๋ ๊ฒ์ ์์ง ๋ง์ญ์์ค.
const html2canvas = require('html2canvas/dist/html2canvas.js');
์ํ ๋น๋๋ฅผ ์ค์นํ ์ ์๊ณ ํจ์น ํจํค์ง๊ฐ ์๋ํ์ง ์์ต๋๋ค. ์ด๊ฒ์ ๊ตฌํํ๋ ๋ฐฉ๋ฒ์ ์๋์?
๋ด ์ํฉ์ ์ ํฉํ ํด๊ฒฐ ๋ฐฉ๋ฒ์ ๋ฐฐ๊ฒฝ ์ด๋ฏธ์ง๋ฅผ ์ฌ์ฉํ๋ ๊ฒ์ ๋๋ค.
์๋ ํ์ธ์, ์ ๋ ์ด๊ฒ์ด ํ์ํ๊ณ ์ ํ๋ก์ ํธ์์ ์๋ํ๋๋ก ํดํนํ์์ ์๋ ค๋๋ฆฝ๋๋ค. ์๋์์ ์ ์๊ฒ ๋ง๋ ์ฝ๋๋ฅผ ์ ๊ณตํฉ๋๋ค. ๊ทธ๋ฌ๋ ์ด๊ฒ์ ํดํน์ ๋๋ค. ์ ๋ ์ด๊ฒ์ ๋ณด๋ค ์ ์ ํ ๋ฐฉ์์ผ๋ก ํ๋ก์ ํธ์ ํตํฉํ ์ง์์ด ์์ต๋๋ค.
key: 'drawImage', value: function drawImage(image, source, destination) { /**START CUSTOM CODE**/ var newWidth = 30; var newHeight = 30; var newX = destination.left; var newY = destination.top; // console.log(image, source, destination); if (source.width/destination.width > source.height/destination.height) { newWidth = destination.width; newHeight = source.height * (destination.width / source.width); newY = destination.top + (destination.height - newHeight) / 2; } else { newWidth = source.width * (destination.height / source.height); newHeight = destination.height; newX = destination.left + (destination.width - newWidth) / 2; } // console.log(newWidth, newHeight); this.ctx.drawImage(image, source.left, source.top, source.width, source.height, newX, newY, newWidth, newHeight); // destination.width, // destination.height * (source.height / source.width) // destination.width, destination.height); /**END CUSTOM CODE**/ }
์ด ์ฝ๋๋ฅผ ์ด๋์ ๋ถ์ฌ๋ฃ์ด์ผ ํ๋์?
์ฝ๊ฐ์ ๋ง์ง์๊ฑฐ๋ฆฌ๋ค๊ฐ ํ์ฌ ๋ฒ์ ์์ ์ ๋ชฉ์ ์ ๋ง๊ฒ ์๋ํ๋๋ก ํ์ต๋๋ค.
CanvasRenderer.prototype.renderReplacedElement = function (container, curves, image) {
if (image && container.intrinsicWidth > 0 && container.intrinsicHeight > 0) {
var box = contentBox(container);
/*CUSTOM CODE*/
var newWidth = 30;
var newHeight = 30;
var newX = box.left;
var newY = box.top;
if (container.intrinsicWidth / box.width < container.intrinsicHeight / box.height) {
newWidth = box.width;
newHeight = container.intrinsicHeight * (box.width / container.intrinsicWidth);
newY = box.top + (box.height - newHeight) / 2;
}
else {
newWidth = container.intrinsicWidth * (box.height / container.intrinsicHeight);
newHeight = box.height;
newX = box.left + (box.width - newWidth) / 2;
}
var path = calculatePaddingBoxPath(curves);
this.path(path);
this.ctx.save();
this.ctx.clip();
this.ctx.drawImage(image, 0, 0, container.intrinsicWidth, container.intrinsicHeight, newX, newY, newWidth, newHeight);
this.ctx.restore();
}
};
์ถ์๋์ง ์์ ํ์ผ์์ ์ฒ์ ๋ช ์ค์ ๊ฒ์ํฉ๋๋ค. ๊ธฐ๋ณธ ์ฝ๋์ ๋ํด @LanderBeeuwsaert ์๊ฒ ๊ฐ์ฌ๋๋ฆฝ๋๋ค.
@canoncarlson ๋ฐ๊ฐ์ต๋๋ค. ์ ๋ฐ์ดํธํด์ฃผ์ ์ ๊ฐ์ฌํฉ๋๋ค. ์ด๊ฒ ๋๋ฌธ์ ์ฐ๋ฆฌ๋ ๊ทธ๊ฒ์ ํตํฉํ๊ณ ์์ผ๋ก ๋ช ์ฃผ ์์ ์ ๋ฐ์ดํธํ ์ ์์ ๊ฒ์ ๋๋ค.
๋๋ ๊ทธ๊ฒ์ ์๋์ํฌ ์ ์์๊ณ , 6274 ๋ผ์ธ ์ฃผ์์ node_modules/html2canvas/dist/html2canvas.js์ CanvasRenderer.prototype.renderReplacedElement๋ฅผ ๋ณ๊ฒฝํ์ต๋๋ค. ์ ๋ RC 5 ๋ฒ์ ์ ์ฌ์ฉํ๊ณ ์์ต๋๋ค. ๊ธฐ๋ณธ์ ์ผ๋ก @canoncarlson ๊ณผ ๋์ผํ ์ฝ๋์ ๋๋ค. ๊ฐ์ฒด ๋ง์ถค ๋ฎ๊ฐ๊ฐ ์ด ์์ ๊ณผ ํจ๊ป ์๋ํ๋์ง ํ์ธ
CanvasRenderer.prototype.renderReplacedElement = function (container, curves, image) {
if (image && container.intrinsicWidth > 0 && container.intrinsicHeight > 0) {
var box = contentBox(container);
var path = calculatePaddingBoxPath(curves);
this.path(path);
this.ctx.save();
this.ctx.clip();
let newWidth;
let newHeight;
let newX = box.left;
let newY = box.top;
if(container.intrinsicWidth / box.width < container.intrinsicHeight / box.height) {
newWidth = box.width;
newHeight = container.intrinsicHeight * (box.width / container.intrinsicWidth);
newY = box.top + (box.height - newHeight) / 2;
} else {
newWidth = container.intrinsicWidth * (box.height / container.intrinsicHeight);
newHeight = box.height;
newX = box.left + (box.width - newWidth) / 2;
}
this.ctx.drawImage(image, 0, 0, container.intrinsicWidth, container.intrinsicHeight, newX, newY, newWidth, newHeight);
this.ctx.restore();
}
};
์ด๊ธฐ ์๋ฃจ์ ์ ์ ๊ณตํ @LanderBeeuwsaert ์๊ฒ ๊ฐ์ฌ๋๋ฆฌ๋ฉฐ ์ด ์์ ์ฌํญ์ด ๊ณง ๊ธฐ๋ณธ ๋ฒ์ ์ผ๋ก ์ ๊ณต๋๊ธฐ๋ฅผ ๋ฐ๋๋๋ค.
๊ฐ์ฅ ์ ์ฉํ ๋๊ธ
์๋ ํ์ธ์, ์ ๋ ์ด๊ฒ์ด ํ์ํ๊ณ ์ ํ๋ก์ ํธ์์ ์๋ํ๋๋ก ํดํนํ์์ ์๋ ค๋๋ฆฝ๋๋ค. ์๋์์ ์ ์๊ฒ ๋ง๋ ์ฝ๋๋ฅผ ์ ๊ณตํฉ๋๋ค. ๊ทธ๋ฌ๋ ์ด๊ฒ์ ํดํน์ ๋๋ค. ์ ๋ ์ด๊ฒ์ ๋ณด๋ค ์ ์ ํ ๋ฐฉ์์ผ๋ก ํ๋ก์ ํธ์ ํตํฉํ ์ง์์ด ์์ต๋๋ค.
```
ํค: '๊ทธ๋ฆฌ๊ธฐ ์ด๋ฏธ์ง',
๊ฐ: function drawImage(์ด๋ฏธ์ง, ์์ค, ๋์) {
/ ์ฌ์ฉ์ ์ ์ ์ฝ๋ ์์ /
```