uniapp开发微信小程序实现canvas画圆形

直接copy代码即可(感兴趣可扫码看看)
在这里插入图片描述

<template>
	<view class="canvas-content">
		<!-- 画布 -->
    <canvas
      class="mycanvas"
      disable-scroll="true"
      canvas-id="Canvas"
      id="Canvas"
    >
    </canvas>
	</view>
</template>
<script>
	export default {
		data() {
			return {
        ctx: null
			}
		},
		onLoad() {
      this.ctx = uni.createCanvasContext("Canvas")
		},
    onShow () {
      this.drawArc()
    },
		methods: {
      drawArc () {
        // 非填充圆
        this.ctx.beginPath();
        this.ctx.arc(50, 50, 20, 0, 2 * Math.PI);
        this.ctx.setStrokeStyle('red');
        this.ctx.stroke();
        this.ctx.draw(true);

        // 填充圆
        this.ctx.beginPath();
        this.ctx.arc(50, 100, 20, 0, 2 * Math.PI);
        this.ctx.setFillStyle('red');
        this.ctx.fill();
        this.ctx.draw(true);
      }
		}
	}
</script>
  • 6
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
您好!如果您想要将 Uniapp 微信小程序中的界面转换为图片,可以尝试使用以下方法: 1. 使用 `uni.canvasToTempFilePath()` 方法将界面转换为图片 Uniapp 中提供了 `uni.canvasToTempFilePath()` 方法,可以将指定的 canvas 区域内容转换为临时文件路径。因此,我们可以通过将整个页面的内容绘制到一个 canvas 中,再将 canvas 转换为图片,来实现将界面转换为图片的功能。 下面是一个简单的示例代码: ```javascript // 获取 canvas 上下文 const query = uni.createSelectorQuery() query.select('#myCanvas').fields({ node: true, size: true }) .exec((res) => { const canvas = res[0].node const ctx = canvas.getContext('2d') // 绘制整个页面内容到 canvas 上 const page = getCurrentPages()[getCurrentPages().length - 1] const width = page.windowWidth const height = page.windowHeight const dpr = uni.getSystemInfoSync().pixelRatio canvas.width = width * dpr canvas.height = height * dpr ctx.scale(dpr, dpr) uni.renderPage(page).then(() => { uni.drawCanvas({ canvasId: 'myCanvas', x: 0, y: 0, width: width, height: height, destWidth: width * dpr, destHeight: height * dpr, }) }) // 将 canvas 转换为临时文件路径 uni.canvasToTempFilePath({ canvasId: 'myCanvas', success: (res) => { console.log(res.tempFilePath) // 可以将临时文件路径保存到本地或者上传到服务器 }, }) }) ``` 在上面的代码中,我们首先获取了当前页面的 canvas 上下文,并将整个页面的内容绘制到了 canvas 上。然后,我们使用 `uni.canvasToTempFilePath()` 方法将 canvas 转换为临时文件路径,最后可以将临时文件路径保存到本地或者上传到服务器。 2. 注意事项 需要注意的是,如果您的界面中包含了涉及用户隐私的内容(例如输入框、密码框等),则不应将整个界面转换为图片。另外,由于微信小程序的安全限制,界面转换为图片的功能在某些场景下可能会受到限制,具体情况需要根据实际测试结果而定。 希望以上内容能够帮助到您,如果您还有其他问题或者需要更详细的解答,请随时提出。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值