Html5 Canvas一个简单的画笔例子
相比了下Qt quick的canvas和HTML5的canvas,发现HTML5 Canvas在同样绘制绘制操作下性能比Qt的canvas强很多,附上一个HTML5 canvas画笔一例子
var DW = function( canvasid){
this._points = [];
this._canvas = document.getElementById( canvasid );
this._ctx = this._canvas.getContext("2d");
this._isPressed = false;
this._color = "#223344";
this._widht = 8;
var self = this;
function __init(){
self._ctx.lineCap = "round";
self._ctx.lineJoin="round";
self._ctx.strokeStyle = self._color;
self._ctx.lineWidth = self._widht;
};
this._addPoints = function( x, y ){
this._points.push({x: x , y : y });
};
this._capturePath = function( x , y ){
this._addPoints( x, y );
};
this._prepareDrawing = function( x, y ){
this._points.length = 0 ;
this._addPoints( x, y );
this._ctx.moveTo( x, y );
};
this._render = function(){
console.log("renderrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr");
var p1 = this._points[0] , p2 =this._points[1];
this._ctx.save();
this._ctx.beginPath();
if(this._points.length === 2 && p1.x === p2.x && p1.y===p2.y ){
p1.x -= 0.5 ;
p1.y -= 0.5 ;
}
this._ctx.moveTo( p1.x , p1.y );
for( var i = 1 , len = this._points.length ; i < len ; i++ ){
var mp = {x : (p1.x + (p2.x - p1.x ) /2) , y : (p1.y + (p2.y - p1.y)/2)};
this._ctx.quadraticCurveTo( p1.x , p1.y , mp.x , mp.y );
p1 = this._points[i] ; p2 = this._points[i+1];
}
this._ctx.lineTo( p1.x , p1.y );
this._ctx.stroke();
this._ctx.restore();
}
this._clearContext = function(){
this._ctx.clearRect(0,0, this._canvas.width , this._canvas.height);
}
__init();
};
DW.prototype.setColor = function(color){
this._color = color;
this._ctx.strokeStyle = color;
};
DW.prototype.setWidth = function( w ){
this._widht = w;
this._ctx.lineWidth = w ;
}
应用
<!doctype html>
<html>
<head>
<title> Canvas </title>
<style type="text/css">
canvas{
border-radius: 8;
border-style: solid;
border-color: 'gray'
}
</style>
<script ==> 引用上头JS文件 =========/script>
</head>
<body>
<canvas id="_canvas" width='500' height="400" ></canvas>
<script type="text/javascript">
var $ = function( id ){ return document.getElementById(id);}
var c = $('_canvas');
var dObject = new DW("_canvas");
function on_mouse_press(e){
dObject.setColor("#778899");
dObject._prepareDrawing(e.offsetX,e.offsetY);
dObject._capturePath(e.offsetX,e.offsetY);
dObject._render(); dObject._isPressed = true;
}; function on_mouse_move( e) {
if( dObject._isPressed === true ){
dObject._capturePath(e.offsetX,e.offsetY);
dObject._clearContext();
dObject._render(); //redraw
}
}; function on_mouse_up(e){
dObject._isPressed = false;
}
c.addEventListener('mousedown',function(e){
on_mouse_press(e);
},false); c.addEventListener('mousemove' , function(e){
on_mouse_move(e);
},false); c.addEventListener('mouseup',function(e){
on_mouse_up(e);
},false);
</script>
</body>
</html>
Html5 Canvas一个简单的画笔例子的更多相关文章
- <html5 canvas>一个简单的矩形
Html5: <!doctype html> <html> <head> <meta charset="UTF-8"> <ti ...
- 学习笔记:HTML5 Canvas绘制简单图形
HTML5 Canvas绘制简单图形 1.添加Canvas标签,添加id供js操作. <canvas id="mycanvas" height="700" ...
- 使用Multiplayer Networking做一个简单的多人游戏例子-2/3(Unity3D开发之二十六)
猴子原创,欢迎转载.转载请注明: 转载自Cocos2Der-CSDN,谢谢! 原文地址: http://blog.csdn.net/cocos2der/article/details/51007512 ...
- 一个简单的CORBA例子
因为对CORBA分析的需要,这里写一个简单的CORBA例子.从JDK1.2开始,JDK中集成了ORB的实现,本例子使用了JDK1.7,对于JDK1.2+应该都没有问题.这个例子实现一个简单的加减乘除的 ...
- 轻松创建nodejs服务器(1):一个简单nodejs服务器例子
这篇文章主要介绍了一个简单nodejs服务器例子,本文实现了一个简单的hello world例子,并展示如何运行这个服务器,需要的朋友可以参考下 我们先来实现一个简单的例子,hello world ...
- 使用Multiplayer Networking做一个简单的多人游戏例子-3/3(Unity3D开发之二十七)
使用Multiplayer Networking做一个简单的多人游戏例子-1/3 使用Multiplayer Networking做一个简单的多人游戏例子-2/3 使用Multiplayer Netw ...
- 使用Multiplayer Networking做一个简单的多人游戏例子-1/3(Unity3D开发之二十五)
猴子原创,欢迎转载.转载请注明: 转载自Cocos2Der-CSDN,谢谢! 原文地址: http://blog.csdn.net/cocos2der/article/details/51006463 ...
- 一个简单的cmake例子
一个简单的cmake例子CMakeLists.txt,生成动态库文件,可以指定发布目录. 尚不支持: 1.交叉编译环境配置 2.添加依赖库 #在当前目录新建一个build目录,然后cd build ...
- html5 canvas 实现简单的画图
今天早上看了一下 canvas 前端画图,数据可视化, 百度的 echart.js , d3等 js 库都已经提供了强大的绘制各种图形的 API. 下面记录一下 有关canvas 绘图的基本知识: ...
随机推荐
- 一张图看Goodle Clean设计架构
之前用一张图分析了Google给出的MVP架构,但是在Google给出的所有案例里面除了基本的MVP架构还有其它几种架构,今天就来分析其中的Clean架构.同样的,网上介绍Clean架构的文章很多,我 ...
- 超图(hypergraph)
超图是什么? 超图的本质特征在于它的超边,它可以连接两个以上的结点(包括两个).按这样的意义来说,我们所熟悉的普通图只是超图的一个特例而已,而超图则定义了一个更加宽泛的图. 超图的数学定义为:对于超图 ...
- Orchard官方文档翻译(一) 总览
原文地址:http://docs.orchardproject.net/ 最近想要学习了解orchard,但却没有找到相关的中文文档,只有英文文档.于是决定自行翻译,以便日后方便翻阅. 转载请注明原作 ...
- How to deploy JAVA Application on Azure Service Fabric
At this moment, Azure Service Fabric does not support JAVA application natively (but it's on the sup ...
- No.005 Longest Palindromic Substring
5. Longest Palindromic Substring Total Accepted: 120226 Total Submissions: 509522 Difficulty: Medium ...
- windows对象分类
用户对象 GDI对象 内核对象 如何判断一个对象是不是内核对象 看创建函数,几乎所有的内核对象创建函数都会有PSECURITY_ATTRIBUTES参数
- ios项目记录
1,如何隐藏状态栏 在基类中重载UIViewController.h中的这个方法 - (BOOL)prefersStatusBarHidden { // iOS7后,[[UIApplication s ...
- [JFinal 1] JFinal和SSH中使用拦截器的对比
导读:先前在做.NET项目时,拦截的功能主要是依靠缓存session来实现.当需要跳转到某个页面,使用某个功能查询一些数据时,会根据session中的用户值来判断是否已经正常登录,如果没有,则重定向到 ...
- ios学习开发阶段小结
总结一下,开发了1个月10天的ios经验. 先晒成绩单:两个实验性质的app,一个wifi管家,一个图片壁纸软件 技术小结: 1.熟悉基本的各种ns语法:#import,#include,@class ...
- PHP生成静态页面的方法
在PHP网站开发中为了网站推广和SEO等需要,需要对网站进行全站或局部静态化处理,PHP生成静态HTML页面有多种方法,比如利用PHP模板.缓存 等实现页面静态化,今天就以PHP实例教程形 ...