0.0.13 • Published 3 months ago

draw-html-to-canvas v0.0.13

Weekly downloads
-
License
ISC
Repository
-
Last release
3 months ago

draw html to canvas

使用html+css语法绘制图片到canvas上

安装

npm i draw-html-to-canvas --save

渲染效果

电脑端渲染效果图
上半部分为浏览器效果
下半部分为渲染效果

npm.io

微信开发者工具渲染效果图
npm.io

微信真机渲染效果图 npm.io

使用方法

纯页面使用方法

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>draw html to canvas</title>
  <style>
    * {
      margin: 0;
      padding: 0;
    }

    #canvas {
      width: 100%;
      height: 100%;
    }
  </style>
</head>
<body>
<canvas id="canvas"></canvas>
</body>
<script src="node_modules/draw-html-to-canvas/dist/index.umd.js"></script>
<script>
  ;(async function() {
    const DrawHtml2Canvas = window.DrawHtml2Canvas.default;

    const html = `<div style="border: 1px solid #f00">draw to canvas</div>`;

    const render = DrawHtml2Canvas.fromHTML(html);

    // 设置网页最大宽度
    render.rootNode.style.set('width', '500px');

    const canvas = document.querySelector('#canvas');
    const ctx = canvas.getContext('2d');

    // 加载html中使用的图片
    await render.loadSource();

    // 计算布局
    render.layout(ctx);

    // 获取网页尺寸
    const {offsetWidth, offsetHeight} = render.rootNode;

    // 高清适配
    ctx.save();
    canvas.height = offsetHeight * window.devicePixelRatio;
    canvas.width = offsetWidth * window.devicePixelRatio;
    ctx.scale(window.devicePixelRatio, window.devicePixelRatio);

    // 绘制图像到canvas上
    render.draw(ctx);

    ctx.restore();
  })();
</script>
</html>

web工程使用方法

import Render from 'draw-html-to-canvas';

const html = `<div>draw to canvas</div>`
const render = Render.fromHTML(html);

const canvas = document.querySelector('#canvas');
const ctx = canvas.getContext('2d');

// 修改网页最大宽度为500px
render.rootNode.style.set('width', '500px');

// 加载html中使用的图片
await render.loadSource();

// 计算布局
render.layout(ctx);

// 获取网页尺寸
const {offsetWidth, offsetHeight} = render.rootNode;

// 高清适配
ctx.save();
canvas.height = offsetHeight * window.devicePixelRatio;
canvas.width = offsetWidth * window.devicePixelRatio;
ctx.scale(window.devicePixelRatio, window.devicePixelRatio);

// 绘制图像到canvas上
render.draw(ctx);

ctx.restore();

微信小程序

渲染效果的截图的代码片段
最低支持1.9的基础库
2.9以上可以使用同层渲染 推荐使用同层渲染api

在线代码片段

采用float布局系统

block 独占一行
inline-block 行内布局超长自动换行
inline 行内布局超长自动换行
内置了常见的标签识别

支持标签

标签支持默认布局默认行为
divblock
imginline-blockimg元素会自动加载图片
spaninline

支持样式

样式属性有效值特性
displayblock\inline-block\inline
floatleft\right
clear任意值都是both
width绝对值
height绝对值
padding绝对值
margin绝对值\支持 margin: 0 auto; 居中对齐
positionrelative\absolute
top绝对值绝对定位有效
right绝对值绝对定位有效
bottom绝对值绝对定位有效
left绝对值绝对定位有效
z-index同block下所有的值拉平计算优先级
color支持 rgba\rgb#xxx\transparent
border全功能
border-style只支持 solid\dashed
border-radius全功能
background全功能 支持多重背景 简写、全写、支持线性渐变
font-style绝对值
font-variant绝对值
font-weight绝对值
font-stretch绝对值
font-size绝对值
font-family绝对值
text-decoration全写、简写
text-decoration-colorcolor支持的范围
text-decoration-style只支持 solid\double
text-decoration-thichness绝对值
text-decoration-lineunderline\overline\line-through
line-height固定值、倍数
text-align全功能
opacity全功能

LiveDemo

在线示例

0.0.13

3 months ago

0.1.1

8 months ago

0.1.0

2 years ago

0.0.11

2 years ago

0.0.12

2 years ago

0.0.10

2 years ago

0.0.9

2 years ago

0.0.8

2 years ago

0.0.7

2 years ago

0.0.6

2 years ago

0.0.5

2 years ago

0.0.4

2 years ago

0.0.3

2 years ago

0.0.2

2 years ago

0.0.1

2 years ago