前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >oCanvas 一款以对象的方式操作canvas

oCanvas 一款以对象的方式操作canvas

作者头像
前Thoughtworks-杨焱
发布2021-12-08 08:44:23
2520
发布2021-12-08 08:44:23
举报
文章被收录于专栏:杨焱的专栏

oCanvas changes the way you work with the canvas element. With the native canvas API you are drawing pixels onto the canvas. But when building something, you tend to think more in terms of objects than actual pixels. This is what oCanvas is made to do — create a bridge between the native API and the way you want to work. If you would want to access the native API, that is also possible.

http://ocanvas.org/docs

例子代码:

代码语言:javascript
复制
<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>oCanvas Demo</title>
	<script src="ocanvas-2.7.4.min.js"></script>
	<style type="text/css">
		
	</style>
</head>
<body>
	<canvas id="c1" width="1000" height="600"></canvas>
	<script type="text/javascript">

	// 创建一个Canvas画布对象
		var canvas = oCanvas.create({
			canvas:"#c1",
			background:'#000000'
		});

		// Center planet
		var center = canvas.display.ellipse({
			x:canvas.width/2,
			y:canvas.height/2,
			radius:canvas.width/20,
			fill:'#fff'
		}).add();


		// Prototype objects that will be used to instantiate the others
		
		var satelliteProto = canvas.display.ellipse({
			fill:'#eee'
		});

		var pathProto = canvas.display.ellipse({
			stroke:'1px #999'
		});

		// Set up data
		var satellites=[];depth=3;
		var satelliteColors=["#FF0000","#00BC22","#00659D"];
		var pathColors=["#6C6868","#EF1919","#20208F"];
		// Create seven satellites and paths. Definition is further down.
		
		for (var i = 0,l=7; i <l; i++) {
			createSatellite({
				parent:center,
				depth:1,
				distance:(i+1)*canvas.width/6,
				radius:canvas.width/100,
				speed:1
			})
		};

		// Set up a tick function that will move all satellites each time
		
		canvas.setLoop(function () {
			for (var i = 0; i < satellites.length; i++) {
				satellites[i].rotation+=satellites[i].speed;
			};
		});

		// Definition for a satellite and its corresponding path
		function createSatellite (options) {
			// Create the path that the satellite will follow
	var path = pathProto.clone({
		radius: options.distance,
		x: options.x || 0, y: options.y || 0,
		strokeColor: pathColors[options.depth - 1]
	});
	options.parent.addChild(path);

	// Create a new satellite
	var satellite = satelliteProto.clone({
		origin: {
			x: 0,
			y: options.distance * (Math.round(Math.random()) ? 1 : -1)
		},
		speed: Math.random() * (2 * Math.random() - 0.5) + 0.5,
		radius: options.radius,
		x: options.x || 0, y: options.y || 0,
		fill: satelliteColors[options.depth - 1],
		rotation: Math.random() * 360
	});
	options.parent.addChild(satellite);
	satellites.push(satellite);

	// Create another satellite that will circle around this satellite
	if (options.depth < depth) {
		createSatellite({
			parent: satellite, depth: options.depth + 1,
			distance: options.radius * 7,
			radius: options.radius / 1.5,
			x: satellite.origin.x * -1, y: satellite.origin.y * -1,
			speed: 10
		});
	}
		}
canvas.timeline.start();
	</script>
</body>
</html>
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2015-06-17,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档