Kros的博客 Kros的博客
首页
  • CSS
  • 工具
  • Vue
  • js
  • Vue3
  • 算法
  • 折腾笔记
一言
  • 分类
  • 标签
  • 归档
码云

Kros

凡心所向,素履以往,生如逆旅,一苇以航
首页
  • CSS
  • 工具
  • Vue
  • js
  • Vue3
  • 算法
  • 折腾笔记
一言
  • 分类
  • 标签
  • 归档
码云
  • 每日算法

  • 折腾

    • 阿里云效Codeup基础
    • 阿里云效Codeup流水线自动部署
    • code1024
    • 微信使用X5调试H5页面
    • scrcpy投屏,电脑操控手机
    • 使用win+r启动应用程序
    • 使用浏览器启动应用
    • 高德地图infoWindow信息窗中使用vue页面
    • chrome浏览器新标签页自动打开控制台?
    • microapp微前端框架
    • 使用键盘操作浏览器网页
  • 分享

  • 随笔
  • 折腾
kros
2020-11-17

code1024

今天在开源中国 上发现一个有意思的项目:code1024 ,利用纯数学计算和canvas画出一些漂亮的图形。像这样:

示例1 示例2

看到这里我就想自己实现一个,看一下我的代码(uni-app)。

	// html
	//<canvas canvas-id="canvas-p" id="canvas" style="width: 512px; height: 512px;"></canvas>
	// js
	mounted(){
		this.canvasP = document.getElementById("canvas");
		const ctx = uni.createCanvasContext('canvas-p', this);
		for(let i = 0; i < this.canvasP.width; i++){
			for(let j = 0; j < this.canvasP.height; j++){
				ctx.setFillStyle(this.getColorByFunc(i, j));
				ctx.fillRect(i, j, 1, 1);
			}
		}	
		ctx.draw();
		uni.canvasToTempFilePath({
			x: 0,
			y: 0,
			width: this.canvasP.width,
			height: this.canvasP.height,
			canvasId: id,
			success: (rest) => {
				var savedFilePath = rest.tempFilePath;//相对路径 base64
				this.src = savedFilePath;
			}
		});
	}
	
	methods: {
		// 传入坐标返回颜色值
		getColorByFunc(x, y){
			//var a=0,b=0,d,n=0,r=0,g=0;
			//for(;a*a+(d=b*b)<4&&n++<4096;b=2*a*b+y/2.5e4+.06,a=a*a-d+x/2.5e4+.34);
			//r=n/4;
			//g=2*r;
			//b=4*r;
			return `rgb(${r},${g},${b})`;
			//return [r,g,b,255];
		},
	}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38

ok!一口气写完运行,....WTF?卡顿将近10秒,结果发现是setFillStyle和fillRect使用太频繁引起的。 技术有限看源码 吧,研究作者的代码发现作者使用了getImageData和putImageData,前者是获取canvas区域的像素数据、后者是把颜色数据绘制到canvas上,有了这两个就可以直接操作像素数据极大的减少对canva本身的操作。修改代码如下:

	this.canvasP = document.getElementById("canvas");
	let width = 512, height = 512;
	// uni-app没有getImageData 但是实现了canvasGetImageData API用法一样
	uni.canvasGetImageData({
		canvasId: 'canvas-p',
		x: 0,
		y: 0,
		width: width,
		height: height,
		success: (res) => {
			let imageData = res.data;
			let index = 0;
			for(let i = 0; i < width; i++){
				for(let j = 0; j < height; j++){
					let rgb = this.getColorByFunc(i, j);// ctx.setFillStyle(this.getColorByFunc(i, j));
					imageData[index++] = rgb[0];
					imageData[index++] = rgb[1];
					imageData[index++] = rgb[2];
					imageData[index++] = rgb[3];					
				}
			}	
			// uni-app没有putImageData
			uni.canvasPutImageData({
				canvasId: 'canvas-p',
				data: imageData,
				x: 0,
				y: 0,
				width: width,
				height: height
			})
		}
	});
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32

运行,完美!没有丝毫卡顿。 总结一下:在uni-app上没法使用dom设置canvas的宽高,所有这里采用style设置同比缩减了一般宽高,在计算颜色代码上与作者有点差别。我还有一个问题,如果把颜色值绘制到canvas上也就是位图(bitmap),那bitmap是怎样转换png或jpg?

#canvas
上次更新: 2025/09/05, 8:09:00
阿里云效Codeup流水线自动部署
微信使用X5调试H5页面

← 阿里云效Codeup流水线自动部署 微信使用X5调试H5页面→

最近更新
01
Find the next perfect square
09-05
02
Regex validate PIN code
09-05
03
Find the odd int
09-05
更多文章>
Theme by Vdoing | Copyright © 2020-2025 kros king
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式
icon-heart-o icon-heart icon-infinity icon-pause icon-play link next prev