Joã*_*ski 5 html javascript css html2canvas
我正在使用html2canvas库来截取 HTML 节点的屏幕截图,但它根本无法识别该clip-path属性。
(我尝试在这里复制错误时遇到跨域问题,所以我做了一个jsfiddle)
https://jsfiddle.net/1Ly9wn6k/
<div id="root">
<div class="star-mask">
<div class="square"></div>
</div>
</div>
<button onclick="capture()">Capture</button>
Run Code Online (Sandbox Code Playgroud)
.star-mask {
clip-path: path('m55,237 74-228 74,228L9,96h240');
}
.square {
background-color: red;
width: 200px;
height: 150px
}
Run Code Online (Sandbox Code Playgroud)
function capture() {
let node = document.querySelector('.star-mask')
html2canvas(node)
.then(canvas => {
document.querySelector('#root').appendChild(canvas)
})
}
Run Code Online (Sandbox Code Playgroud)
每次我单击“捕获”时,画布屏幕截图完全忽略clip-path星形并仅显示红色方块。我已经尝试过html2image库并遇到了同样的问题。
还有其他解决方案可以解决这个问题吗?或者目前无法clip-path使用 JavaScript 捕获属性?
使用dom-to-image对我有用
function capture() {
let node = document.querySelector('.star-mask')
domtoimage.toPng(node)
.then(dataUrl => {
var img = new Image();
img.src = dataUrl;
document.body.appendChild(img);
})
}Run Code Online (Sandbox Code Playgroud)
.star-mask {
clip-path: path('m55,237 74-228 74,228L9,96h240');
}
.square {
background-color: red;
width: 200px;
height: 150px
}Run Code Online (Sandbox Code Playgroud)
<script src="https://cdnjs.cloudflare.com/ajax/libs/dom-to-image/2.6.0/dom-to-image.js"></script>
<div id="root">
<div class="star-mask">
<div class="square"></div>
</div>
</div>
<button onclick="capture()">Capture</button>Run Code Online (Sandbox Code Playgroud)