Html2canvas: div๊ฐ€ object-fit: cover๋กœ ์„ค์ •๋œ ๊ฒฝ์šฐ์—๋„ ์ด๋ฏธ์ง€๊ฐ€ ์ฑ„์›Œ์ง‘๋‹ˆ๋‹ค.

์— ๋งŒ๋“  2017๋…„ 02์›” 27์ผ  ยท  13์ฝ”๋ฉ˜ํŠธ  ยท  ์ถœ์ฒ˜: niklasvh/html2canvas

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**/
    }

```

๋ชจ๋“  13 ๋Œ“๊ธ€

์•ˆ๋…•ํ•˜์„ธ์š”, ์ €๋Š” ์ด๊ฒƒ์ด ํ•„์š”ํ•˜๊ณ  ์ œ ํ”„๋กœ์ ํŠธ์—์„œ ์ž‘๋™ํ•˜๋„๋ก ํ•ดํ‚นํ–ˆ์Œ์„ ์•Œ๋ ค๋“œ๋ฆฝ๋‹ˆ๋‹ค. ์•„๋ž˜์—์„œ ์ €์—๊ฒŒ ๋งž๋Š” ์ฝ”๋“œ๋ฅผ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ ์ด๊ฒƒ์€ ํ•ดํ‚น์ž…๋‹ˆ๋‹ค. ์ €๋Š” ์ด๊ฒƒ์„ ๋ณด๋‹ค ์ ์ ˆํ•œ ๋ฐฉ์‹์œผ๋กœ ํ”„๋กœ์ ํŠธ์— ํ†ตํ•ฉํ•  ์ง€์‹์ด ์—†์Šต๋‹ˆ๋‹ค.

```
ํ‚ค: '๊ทธ๋ฆฌ๊ธฐ ์ด๋ฏธ์ง€',
๊ฐ’: 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 ์—๊ฒŒ ๊ฐ์‚ฌ๋“œ๋ฆฌ๋ฉฐ ์ด ์ˆ˜์ • ์‚ฌํ•ญ์ด ๊ณง ๊ธฐ๋ณธ ๋ฒ„์ „์œผ๋กœ ์ œ๊ณต๋˜๊ธฐ๋ฅผ ๋ฐ”๋ž๋‹ˆ๋‹ค.

์ด ํŽ˜์ด์ง€๊ฐ€ ๋„์›€์ด ๋˜์—ˆ๋‚˜์š”?
0 / 5 - 0 ๋“ฑ๊ธ‰