Canvas -画图
2014-09-30 09:14:57
<!doctype html>
<html> <head>
<title>
</title>
</head> <style> </style>
<body>
<canvas width=="" height="" id="demo">
您的浏览器不支持canvas!
</canvas> <script>
var canvas = document.getElementById('demo');
// alert(canvas); var ctx = canvas.getContext('2d'); //alert(ctx)
ctx.beginPath();
ctx.moveTo(,);
ctx.lineTo(,);
ctx.closePath();
ctx.stroke();
//end
ctx.beginPath();
ctx.moveTo(,);
ctx.lineTo(,);
ctx.closePath();
ctx.stroke();
//end
ctx.beginPath();
ctx.moveTo(,);
ctx.lineTo(,);
ctx.closePath();
ctx.stroke();
//end ctx.beginPath();
ctx.moveTo(,);
ctx.lineTo(,);
ctx.closePath();
ctx.stroke();
//end ctx.beginPath();
ctx.arc(, , , , Math.PI*, true);
ctx.lineWidth = 1.0;
ctx.strokeStyle = "#000";
ctx.closePath();
ctx.stroke(); //左空心眼睛
ctx.beginPath();
ctx.arc(, , , , Math.PI*, true);
ctx.lineWidth = 1.0;
ctx.strokeStyle = "#000";
ctx.closePath();
ctx.stroke();
//右空心眼睛
ctx.beginPath();
ctx.arc(, , , , Math.PI*, true);
ctx.lineWidth = 1.0;
ctx.strokeStyle = "#000";
ctx.closePath();
ctx.stroke(); //右实心眼睛
ctx.beginPath();
ctx.arc(, , , , Math.PI*, true);
ctx.lineWidth = 1.0;
ctx.fillStyle = "#000000";
ctx.fill();
ctx.shadowOffsetX = ; // 设置水平位移
ctx.shadowOffsetY = ; // 设置垂直位移
ctx.shadowBlur = ; // 设置模糊度
ctx.shadowColor = "rgba(0,0,0,1)"; // 设置阴影颜色
ctx.closePath();
ctx.stroke(); //左实心眼睛
ctx.beginPath();
ctx.arc(, , , , Math.PI*, true);
ctx.lineWidth = 1.0;
ctx.strokeStyle = "#000";
ctx.fill();
ctx.shadowOffsetX = ; // 设置水平位移
ctx.shadowOffsetY = ; // 设置垂直位移
ctx.shadowBlur = ; // 设置模糊度
ctx.shadowColor = "rgba(0,0,0,1)"; // 设置阴影颜色
ctx.closePath();
ctx.stroke(); //嘴 ctx.beginPath();
ctx.arc( , , , , false);
ctx.strokeStyle = "#000";
ctx.closePath();
ctx.stroke(); //线 帽子线 ctx.beginPath();
ctx.moveTo(,);
ctx.lineTo(,);
ctx.closePath();
ctx.stroke(); ctx.beginPath();
ctx.moveTo(,);
ctx.lineTo(,);
ctx.closePath();
ctx.stroke(); ctx.beginPath();
ctx.moveTo(,);
ctx.lineTo(,);
ctx.closePath();
ctx.stroke(); ctx.beginPath();
ctx.moveTo(,);
ctx.lineTo(,);
ctx.closePath();
ctx.stroke(); //弧线 开始
- context.arc(x, y, r, startAngle, endAngle, anticlockwise) //语法
其中:
x 代表圆心横坐标
y 代表圆心纵坐标
r 代表弧半径
startAngle 代表起始弧度
endAngle 代表结束弧度
anticlockwise 代表弧的方向,true为逆时针,false为顺时针
//弧线 end
ctx.beginPath();
ctx.arc(300, 300, 130, 310, Math.PI, false);
ctx.strokeStyle = '#000';
ctx.stroke();
ctx.closePath();
</script>
</body>
</html>
Canvas 画文字实例:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Canvas</title>
</head>
<style type="text/css">
body{margin:20px auto; padding:; width:800px; }
canvas{border:dashed 2px #CCC}
</style>
<script type="text/javascript">
function $$(id){
return document.getElementById(id);
}
function pageLoad(){
var can = $$('can');
var cans = can.getContext('2d');
cans.font = 'bold 144px consolas';
cans.textAlign = 'left';
cans.textBaseline = 'top';
cans.strokeStyle = '#DF5326';
cans.strokeText('Hello', , );
cans.font = 'bold 144px arial';
cans.fillStyle = 'red';
cans.fillText('World', ,);
}
function img_click(){
var can = $$('can');
var cans = can.getContext('2d');
cans.clearRect(,,,);
}
</script>
<body onload="pageLoad();">
<canvas id="can" width="800px" height="600px" onclick="img_click(this);"></canvas>
</body>
</html>
Canvas 图像切割
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Canvas</title>
</head>
<style type="text/css">
body{margin:20px auto; padding:; width:800px; }
canvas{border:dashed 2px #CCC}
</style>
<script type="text/javascript">
function $$(id){
return document.getElementById(id);
}
function pageLoad(){
var can = $$('can');
var cans = can.getContext('2d');
var objImg = new Image();
objImg.src = 'lin.jpg';
objImg.onload = function (){
cans.drawImage(objImg,,,,);
}
cans.beginPath();
cans.arc(,,,,Math.PI*,);
cans.closePath();
cans.clip();
}
function img_click(){
var can = $$('can');
var cans = can.getContext('2d');
cans.clearRect(,,,);
}
</script>
<body onload="pageLoad();">
<canvas id="can" width="800px" height="600px" onclick="img_click(this);"></canvas>
</body>
</html> 注: 切出来的图是圆形的。 案例2:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Canvas</title>
</head>
<style type="text/css">
body{margin:20px auto; padding:0; width:800px; }
canvas{border:dashed 2px #CCC}
</style>
<script type="text/javascript">
function $$(id){
return document.getElementById(id);
}
function pageLoad(){
var can = $$('can');
var cans = can.getContext('2d');
var objImg = new Image();
objImg.src = 'lin.jpg';
objImg.onload = function (){
cans.drawImage(objImg,500,400,500,400,100,100,500,400);
}
}
</script>
<body onload="pageLoad();">
<canvas id="can" width="800px" height="600px" onclick="img_click(this);"></canvas>
</body>
</html>
注: 切出来的图是正方形的。
Canvas Api
http://javascript.ruanyifeng.com/htmlapi/canvas.html
参考博文:http://www.cnblogs.com/picaso/archive/2012/11/26/2789077.html
http://www.vaikan.com/html-5-canvas-tutorial-displaying-images/
Canvas -画图的更多相关文章
- html5之canvas画图基础
HTML5+CSS3的好处是,你可以编写一个页面分别用于不同的平台,只需要设置不同的css样式就可以了,现在基本主流浏览器都支持全新的HTML5和CSS3,因为它的跨平台开发.因为是原生代码所以它的页 ...
- Canvas画图在360浏览器中跑偏的问题
问题描述,canvas画图的js代码中编写的是画正方形的代码,结果在360浏览器上变成了长方形,不知道怎么回事,请问各位大神是否遇到过此类问题? <!DOCTYPE html> <h ...
- h5 canvas 画图
h5 canvas 画图 <!DOCTYPE html> <html lang="en"> <head> <meta charset=&q ...
- html5 Canvas画图3:1px线条模糊问题
点击查看原文地址: html5 Canvas画图3:1px线条模糊问题 本文属于<html5 Canvas画图系列教程> 接上一篇canvas画线条教程 上次我们讲到,canvas有时候会 ...
- 使用 canvas 画图时图像文字模糊的解决办法
最近在使用 canvas 画图的时候,遇到了图像文字模糊的问题,解决思路就是根据分辨率创建不同尺寸的画布.以下是创建高分辨率画布的代码: /** * 创建高分辨率画布 * @param w 画布宽 * ...
- html5 canvas 画图移动端出现锯齿毛边的解决方法
使用HTML5的canvas元素画出来的.在移动端手机上测试都发现画图有一点锯齿问题 出现这个问题的原因应该是手机的宽是720像素的, 而这个canvas是按照小于720像素画出来的, 所以在720像 ...
- HTML5 canvas画图
HTML5 canvas画图 HTML5 <canvas> 标签用于绘制图像(通过脚本,通常是 JavaScript).不过,<canvas> 元素本身并没有绘制能力(它仅仅是 ...
- HTML5 Canvas画图与动画学习59例
HTML5 Canvas画图与动画学习59例 学习HTML5 动画,画图的好资料. HTML5 Canvas画图与动画学习59例
- html Canvas 画图 能够选择并能移动
canvas 画图,能够选中所画的图片并且能够随意移动图片 <html xmlns="http://www.w3.org/1999/xhtml"> <head r ...
- 毕业设计总结(1)-canvas画图
去年6月底完成的毕业设计,到现在也才开始给它做个总结,里面有很多可以学习和借鉴的东西. 我的毕业设计的题目是“一种路径规划算法的改进与设计”,具体的要求可参见下面的表格: 题目 一种路径规划算法的改进 ...
随机推荐
- bootstrap--- 两种bootstrap multiselect组件大比拼
http://www.cnblogs.com/landeanfen/p/5013452.html 1.第一种可以兼容IE,第二种不能兼容
- Objective-C中单例模式的实现-备
单例模式在Cocoa和Cocoa Touch中非常常见.比如这两个,[UIApplication sharedApplication]和[NSApplication sharedApplication ...
- Local System/Network Service/Local Service
// The name of the account under which the service should run// 1 NT AUTHORITY\\SYSTEM 2 NT AUTHORIT ...
- 创建Windows服务(Windows Services)N种方式总结
最近由于工作需要,写了一些windows服务程序,有一些经验,我现在总结写出来.目前我知道的创建创建Windows服务有3种方式:a.利用.net框架类ServiceBaseb.利用组件Topshel ...
- js 中比较 undefined
// x has not been declared before if (typeof x === 'undefined') { // evaluates to true without error ...
- U8800安装软件显示无效的URI问题
看到很多人遇到这个问题,其中包括我自己,最后找到可行的解决办法,现整理出来一个新帖,有同样问题的U友可以参考下. 手机先连接电脑,进入USB存储状态,然后在计算机上找到SD卡目录下的.android_ ...
- makefile工程管理
个人理解吧,makefile就是写一个指定格式的文件,将一系列的编译.链接.转换等操作打包在一起,方便以后一键生成可执行的二进制文件而产生的.下面记录一下这种文件的写法,方便以后忘了来查询. make ...
- HDU5141--LIS again (LIS变形)
题意一个序列的LIS为MAX, 求连续子序列的LIS为MAX的个数. 先求出LIS,记录以a[i]结尾的LIS的长度,以及LIS起始位置(靠右的起始位置). 然后线性扫一遍,,线段树与树状数组的差距还 ...
- html基础知识总结2
下拉列表,文本域,复选框 <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> ...
- 公告:本博客搬迁到:http://www.courtiercai.com/
公告: 您好,本人意见本博客搬迁到:http://www.courtiercai.com/.