Commit bbc45333 authored by rockyl's avatar rockyl

canvas支持

parent 7502863a
{
"name": "html-shot",
"version": "1.0.4",
"version": "1.0.6",
"main": "dist/index.js",
"types": "types/index.d.ts",
"license": "MIT",
......
......@@ -21,7 +21,7 @@ document.body.appendChild(img);
| `qaulity` | number | 否 | 0.7 | 导出图片质量 |
## 注意点
* 仅支持img和text两种元素
* 仅支持的元素有:img、text、canvas(svga/spine/lottie)
* 不支持transform变换
* 文本仅只是Arial字体,建议给元素设置为Arial字体
* 不支持z-index样式
......
......@@ -18,14 +18,24 @@ export function parseDom(el: HTMLElement = document.body) {
let nodes = [];
walkNode(el, function (childNode) {
let vNode, bound, node;
if (childNode.nodeName === 'IMG') { //其他全部认为是文本
switch(childNode.nodeName){
case 'IMG':
node = childNode;
vNode = {
type: 1,
src: childNode.src,
}
bound = childNode.getBoundingClientRect();
} else {
break;
case 'CANVAS':
node = childNode;
vNode = {
type: 2,
img: childNode,
}
bound = childNode.getBoundingClientRect();
break;
default:
if (childNode.data) {
node = childNode.parentElement;
let text = childNode.data.trim();
......@@ -41,7 +51,9 @@ export function parseDom(el: HTMLElement = document.body) {
}
}
}
break;
}
if (vNode) {
const {left, top, width, height} = bound;
vNode.x = left - pX;
......@@ -60,6 +72,7 @@ export function parseDom(el: HTMLElement = document.body) {
nodes.push(vNode);
}
});
return {
width,
height,
......
......@@ -12,7 +12,8 @@ interface ICData {
*/
enum NodeType {
TEXT = 0,
IMAGE
IMAGE,
CANVAS,
}
/**
......@@ -78,6 +79,7 @@ export async function toCanvas(data: ICData, options: RenderOptions = {}, callba
nodes.forEach((n) => {
switch (n.type) {
case NodeType.IMAGE:
case NodeType.CANVAS:
drawImage(n, ctx)
break
case NodeType.TEXT:
......
......@@ -29,15 +29,23 @@
font-style: italic;
}
</style>
</head>
<body>
<div id="poster" class="poster">
<img class="avatar" src="//yun.duiba.com.cn/aurora/14e3d0fa0e1ff54553a2c8c094b1caffd90f0a43.png"/>
<canvas id="canvas" style="position: absolute; left: 10px; top: 10px;"></canvas>
<p class="ppp">
a<br/>bcdefghij
</p>
</div>
<script>
let canvas = document.getElementById('canvas');
let context = canvas.getContext('2d');
context.fillRect(0, 0, 50, 30);
</script>
<script src="test.js" type="module"></script>
<button id="button">截图</button>
</body>
</html>
......@@ -4,9 +4,11 @@
import {htmlShot} from "./dist/index.js";
(async function () {
const result = await htmlShot(undefined, {type: 'png'});
async function shot() {
const result = await htmlShot(document.getElementById('poster'), {type: 'png'});
let img = new Image();
img.src = result;
document.body.appendChild(img);
})();
}
document.getElementById('button').onclick = shot;
......@@ -11,7 +11,8 @@ interface ICData {
*/
declare enum NodeType {
TEXT = 0,
IMAGE = 1
IMAGE = 1,
CANVAS = 2
}
/**
* 节点数据
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment