解決html2canvas插件object-fit樣式不生效問題


使用場景

在生成canvas時候需要圖片自適應canvas容器的大小

方法一  將圖片以背景圖方式

<div class="content-img" id="haibaoone" :style="`background: url(`+info.poster_background+`)center center / cover no-repeat;`" > 
                <div class="header_box">
                    <img :src="info.avatar" crossOrigin="anonymous" />
                    <div class="header_name" :style="{color:info.poster_user_color}">{{info.name}}</div>
                </div>
                <div class="qrcode_img">
                    <img :src="qrcode"/>
                </div>
</div>

方法二    給圖片設置相對div的100%的寬高,再設置object-fit:cover;

<div class="content-img" id="haibaoone">
                <img :src="info.poster_background" style="width:100%;height:100%;object-fit:cover;"/>
                <div class="header_box">
                    <img :src="info.avatar" crossOrigin="anonymous" />
                    <div class="header_name" :style="{color:info.poster_user_color}">{{info.name}}</div>
                </div>
                <div class="qrcode_img">
                    <img :src="qrcode"/>
                </div>
            </div>

 

當要生成的html代碼中包含img標簽,並且設置了object-fit:cover屬性后,通過html2canvas生成的圖片object-fit:cover屬性沒有生效,導致生成的圖片與通過樣式設置的不一樣。


解決方法
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();
            //     this.ctx.drawImage(image, 0, 0, container.intrinsicWidth, container.intrinsicHeight, box.left, box.top, box.width, box.height);
            //     this.ctx.restore();
            // }
            // 上面注釋的原來的代碼,下面是我們自己修改后的
            // Start Custom Code
            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();
              }
              // End Custom Code
        };

  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM