内容简介:小程序中的某些场景需要通过 canvas 绘制一些图片, 其中包含一些文字、可能的二维码等等。 然后实现时如果完全手动的去定位元素在 canvas 中的位置,结果就会产生大量不能复用和难以维护的代码。引入 utils 目录下
小程序中的某些场景需要通过 canvas 绘制一些图片, 其中包含一些文字、可能的二维码等等。 然后实现时如果完全手动的去定位元素在 canvas 中的位置,结果就会产生大量不能复用和难以维护的代码。
How to use
引入 utils 目录下 utils/wxml2canvas.js
文件, 这个 repo 本身就是一个简单的示例, 调整一些配置项可以在开发者 工具 中打开。
wxml
<!--
1. wrapper id
2. 需要绘制的元素 className
3. 如果是文本,需要给元素添加加 data-text 属性
-->
<view class="container" id="wrapper">
<text class="title draw" data-text="Hello there">Hello there</text>
<text class="info draw" data-text="小程序是一种新的开放能力,开发者可以快速地开发一个小程序。">
小程序是一种新的开放能力,开发者可以快速地开发一个小程序。
</text>
<view class="image-wrapper draw">
<image class="draw" src="../../assets/demo.jpg"/>
</view>
<button class="generate-btn" bindtap="drawCanvas">generate</button>
</view>
复制代码
wxss
.container {
height: 100%;
box-sizing: border-box;
padding: 10px 20px;
display: flex;
flex-direction: column;
}
.container .title {
font-size:36px;
text-align: left;
margin-bottom: 10px;
}
.container .info {
font-size: 14px;
line-height: 18px;
color: grey;
text-align: left;
margin-bottom: 40px;
}
.container .image-wrapper image {
width: 100%;
}
复制代码
js
Page({
drawCanvas: function() {
const wrapperId = '#wrapper'
const drawClassName = '.draw'
const canvasId = 'canvas-map'
wxml2canvas(wrapperId, drawClassName, canvasId).then(() => {
// canvas has been drawn here, you can save the canvas image with wx.canvasToTempFilePath
})
}
})
复制代码
以上所述就是小编给大家介绍的《One simple way to draw canvas, wxml2canvas》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!
猜你喜欢:本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
Beginning ARKit for iPhone and iPad
Wallace Wang / Apress / 2018-11-5 / USD 39.99
Explore how to use ARKit to create iOS apps and learn the basics of augmented reality while diving into ARKit specific topics. This book reveals how augmented reality allows you to view the screen on ......一起来看看 《Beginning ARKit for iPhone and iPad》 这本书的介绍吧!