html2canvas 中的计算梯度无法正常工作
calculated gradient in html2canvas doesn't work properly
我想在 html2canvas 的帮助下截取一个页面的屏幕截图,该页面的图像由线性渐变覆盖。图像的高度会变化,但宽度固定为 210px,所以我需要使用 calc 来计算渐变的位置,这并不是它在屏幕上看起来的渲染方式。
静态值示例:https://jsfiddle.net/vpj3bz7s/1/
.linearGradient {
height: 200px;
width: 210px;
background-image: linear-gradient(to top left,
yellow 0%,
yellow 80px,
red 80px,
red 110px,
yellow 110px,
yellow 100%);
}
html2canvas(document.body).then(function(canvas) {
document.body.appendChild(canvas);
}
);
<div class="linearGradient"></div>
计算值示例:https://jsfiddle.net/dk309pf6/2/
.linearGradient {
height: 200px;
width: 210px;
background-image: linear-gradient(to top left,
yellow 0%,
yellow calc(50% - 10px),
red calc(50% - 10px),
red calc(50% + 10px),
yellow calc(50% + 10px),
yellow 100%);
}
html2canvas(document.body).then(function(canvas) {
document.body.appendChild(canvas);
}
);
<div class="linearGradient"></div>
编辑:
叠加在渐变上的实际图像如下所示:
截图中的样子是这样的:
我实际代码的JSfiddle如下(不过截图和我原来的有点不一样)
这里换个思路得到同样的梯度。有很多方法,但以下是唯一适用于 html2canvas 的方法:
body {
margin: 0px;
}
.linearGradient {
height: 200px;
width: 210px;
background-color:red;
overflow:hidden;
position:relative;
}
.linearGradient::before,
.linearGradient::after {
content:"";
position:absolute;
top:0;
left:0;
bottom:0;
right:0;
background-repeat:no-repeat;
}
.linearGradient::before {
background:linear-gradient(to bottom right,yellow 49%, transparent 50%);
bottom:10px;
right:10px;
}
.linearGradient::after {
background:linear-gradient(to top left,yellow 49%, transparent 50%);
top:10px;
left:10px;
}
<div class="linearGradient"></div>
使用 html2canvas 的工作代码:
我想在 html2canvas 的帮助下截取一个页面的屏幕截图,该页面的图像由线性渐变覆盖。图像的高度会变化,但宽度固定为 210px,所以我需要使用 calc 来计算渐变的位置,这并不是它在屏幕上看起来的渲染方式。
静态值示例:https://jsfiddle.net/vpj3bz7s/1/
.linearGradient {
height: 200px;
width: 210px;
background-image: linear-gradient(to top left,
yellow 0%,
yellow 80px,
red 80px,
red 110px,
yellow 110px,
yellow 100%);
}
html2canvas(document.body).then(function(canvas) {
document.body.appendChild(canvas);
}
);
<div class="linearGradient"></div>
计算值示例:https://jsfiddle.net/dk309pf6/2/
.linearGradient {
height: 200px;
width: 210px;
background-image: linear-gradient(to top left,
yellow 0%,
yellow calc(50% - 10px),
red calc(50% - 10px),
red calc(50% + 10px),
yellow calc(50% + 10px),
yellow 100%);
}
html2canvas(document.body).then(function(canvas) {
document.body.appendChild(canvas);
}
);
<div class="linearGradient"></div>
编辑: 叠加在渐变上的实际图像如下所示:
截图中的样子是这样的:
我实际代码的JSfiddle如下(不过截图和我原来的有点不一样)
这里换个思路得到同样的梯度。有很多方法,但以下是唯一适用于 html2canvas 的方法:
body {
margin: 0px;
}
.linearGradient {
height: 200px;
width: 210px;
background-color:red;
overflow:hidden;
position:relative;
}
.linearGradient::before,
.linearGradient::after {
content:"";
position:absolute;
top:0;
left:0;
bottom:0;
right:0;
background-repeat:no-repeat;
}
.linearGradient::before {
background:linear-gradient(to bottom right,yellow 49%, transparent 50%);
bottom:10px;
right:10px;
}
.linearGradient::after {
background:linear-gradient(to top left,yellow 49%, transparent 50%);
top:10px;
left:10px;
}
<div class="linearGradient"></div>
使用 html2canvas 的工作代码: