網站首頁 編程語言 正文
我本來給圖片設置了object-fit:cover;
但是 html2canvas畫完之后 竟然消失了
這個就很奇怪了,包括圖片的圓角也沒有了,
我翻了翻一些文檔說
- 子元素設置相對于父元素寬高 然后在設置object-fit (我試了試不行pass掉了)
- 就是通過設置父元素彈性布局。什么鬼我試了試也是不行
- 最后看一個博客是 修改源碼 最后成功了。
本來想找到那篇博客呢。后來找不到 但是代碼我這邊還有保存
我的html2canvas和那個博主的版本還不相同但是代碼 沒變多少
找到你的項目中的node_modules/html2canvas/dist/html2canavs.js文件
然后搜索
CanvasRenderer.prototype.renderReplacedElement
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();
// }
// 改成下面的代碼
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();
}
};
最后保存就可以。
大功告成。
關注我 持續更新前端 知識。
原文鏈接:https://yunchong.blog.csdn.net/article/details/121756284
相關推薦
- 2023-05-07 GO中什么情況會使用變量逃逸_Golang
- 2022-09-03 Python實現計算AUC的三種方式總結_python
- 2022-11-14 mac 常用終端命令
- 2022-06-02 C語言實現簡單的抽獎系統_C 語言
- 2022-05-06 Python?ORM框架之SQLAlchemy?的基礎用法_python
- 2022-05-24 Pytho的HTTP交互httpx包模塊使用詳解_python
- 2022-03-14 flutter ‘package:flutter/src/widgets/container.dar
- 2022-07-09 Android同步異步任務與多線程和Handler消息處理機制
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支