canvas基本画图
<img src="img/lamp.gif" id="lamp"/>
<img src="img/eg_tulip.jpg" id="tulip"/>
<!-- <video id="video" autoplay controls>
<source src="img/mov_bbb.mp4" type="video/mp4"/>
</video> -->
<canvas id="canvas" height="300" width="300" style="border:1px solid #d3d3d3;"></canvas>
<script type="text/javascript">
var c=document.getElementById("canvas");
var ctx=c.getContext("2d");
//填充单一颜色
//ctx.rect(20,20,200,400);
//ctx.fillStyle="red";
//ctx.fill();
//填充线性渐变颜色
// var gradient=ctx.createLinearGradient(0,0,0,100);
// gradient.addColorStop(0,"red");
// gradient.addColorStop(1,"green");
// ctx.fillStyle=gradient;
// ctx.fillRect(20,20,150,100);
//填充背景图片
// window.onload=function(){
// draw("repeat");
// }
// function draw(direction){
// var img=document.getElementById("lamp")
// var pat=ctx.createPattern(img,direction);
// ctx.rect(0,0,150,100);
// ctx.fillStyle=pat;
// ctx.fill();
// }
//画矩形
//ctx.strokeStyle="red";
// ctx.strokeRect(20,20,200,150);
//线性渐变色矩形
// var gradient=ctx.createLinearGradient(0,0,170,0);
// gradient.addColorStop(0,"red");
// gradient.addColorStop(0.5,"blue");
// gradient.addColorStop(1,"green");
// ctx.lineWidth=5;
// ctx.strokeStyle=gradient;
// ctx.strokeRect(20,20,200,200);
//线性渐变文字
// ctx.font="30px Microsoft YaHei";
// var gradient=ctx.createLinearGradient(0,0,200,10);
// gradient.addColorStop(0,"red");
// gradient.addColorStop(0.5,"blue");
// gradient.addColorStop(1,"green");
// ctx.strokeStyle=gradient;
// ctx.strokeText("Hello Word",20,30);
//绘制带黑色阴影的蓝色矩形
// ctx.shadowBlur=20;
// ctx.shadowOffsetX=10;
// ctx.shadowOffsetY=10;
// ctx.shadowColor="black";
// ctx.fillStyle="blue";
// ctx.fillRect(20,20,100,150);
//放射状/圆形渐变
// var grd=ctx.createRadialGradient(75,50,5,90,60,100);
// grd.addColorStop(0,"red");
// grd.addColorStop(1,"blue");
// ctx.fillStyle=grd;
// ctx.fillRect(10,10,150,100);
//设置或返回线条的结束端点样式lineCap
// ctx.beginPath();
// ctx.lineCap="round";
// ctx.moveTo(20,20);
// ctx.lineTo(200,20);
// ctx.lineWidth=10;
// ctx.stroke();
// ctx.beginPath();
// ctx.lineCap="butt";
// ctx.moveTo(20,40);
// ctx.lineTo(200,40);
// ctx.lineWidth=10;
// ctx.stroke();
// ctx.beginPath();
// ctx.lineCap="square";
// ctx.moveTo(20,60);
// ctx.lineTo(200,60);
// ctx.lineWidth=10;
// ctx.stroke();
//设置或返回两条线相交时,所创建的拐角类型lineJoin:bevel/round/miter
//bevel 创建斜角。
//round 创建圆角。
//miter 默认。创建尖角。
// ctx.lineJoin="bevel";
// ctx.lineWidth=10;
// ctx.moveTo(20,20);
// ctx.lineTo(100,50);
// ctx.lineTo(20,100);
// ctx.stroke();
//设置或返回最大斜接长度miterLimit
// ctx.lineJoin="miter";
// ctx.lineWidth=10;
// ctx.miterLimit=5;
// ctx.moveTo(20,20);
// ctx.lineTo(50,27);
// ctx.lineTo(20,34);
// ctx.stroke();
//画三角形
// ctx.beginPath();
// ctx.moveTo(20,20);
// ctx.lineTo(20,70);
// ctx.lineTo(50,70);
// ctx.strokeStyle="green";
// ctx.closePath();
// ctx.stroke();
//clip从原始画布剪切任意形状和尺寸的区域
// ctx.rect(50,20,200,120);
// ctx.stroke();
// ctx.clip();
// ctx.fillStyle="green";
// ctx.fillRect(0,0,150,200);
//画圆
// ctx.beginPath();
// ctx.arc(100,100,50,0,2*Math.PI);
// ctx.fillStyle="red";
// ctx.stroke();
// ctx.fill();
//创建两切线之间的弧/曲线
// ctx.beginPath();
// ctx.moveTo(20,20);
// ctx.lineTo(100,20);
// ctx.arcTo(150,20,150,70,50);
// ctx.lineTo(150,120);
// ctx.stroke();
//isPointInPath如果指定的点位于当前路径中,则返回 true,否则返回 false
// ctx.rect(20,20,150,200);
// if(ctx.isPointInPath(20,50)){
// ctx.stroke();
// }
//剪切图片,并在画布上对被剪切的部分进行定位:
// document.getElementById("tulip").onload=function(){
// var img=document.getElementById("tulip");
// ctx.drawImage(img,90,180,90,80,20,20,150,200);
// }
//在画布上播放视频
// var video=document.getElementById("video");
// video.addEventListener("play",function(){
// var i=window.setInterval(function(){
// ctx.drawImage(video,0,0);
// },20);
// },false);
// video.addEventListener("pause",function(){
// window.clearInterval(i);
// },false);
// video.addEventListener("ended",function(){
// clearInterval(i);
// },false);
//设置或返回新图像如何绘制到已有的图像上
//source-over,source-atop,source-in,source-out
//destination-over,destination-atop,destination-in,destination-out
//lighter,copy
ctx.fillStyle="red";
ctx.fillRect(0,0,50,50);
ctx.globalCompositeOperation="source-over";
ctx.fillStyle="blue";
ctx.fillRect(20,20,50,50);
//save保存上下文环境
ctx.save();
ctx.shadowOffsetX=10;
ctx.shadowOffsetY=10;
ctx.shadowBlur=5;
ctx.shadowColor="black";
//restore用于恢复到上一次保存的上下文环境
ctx.fillStyle="red";
ctx.fillRect(0,0,50,50);
ctx.restore();
ctx.fillStyle="green";
ctx.fillRect(80,0,50,50);
上面代码先用save方法,保存了当前设置,然后绘制了一个有阴影的矩形。接着,使用restore方法,恢复了保存前的设置,绘制了一个没有阴影的矩形。
canvas基本画图的更多相关文章
- canvas 在线画图
canvas 在线画图 <!DOCTYPE html> <html lang="en"> <head> <meta charset=&qu ...
- 使用Canvas制作画图工具
前 言 JRedu canvas是HTML5中重要的元素之一,canvas元素使用JavaScript在网页上绘制图像,画布是一个矩形区域,我们可以控制其每一个元素,并且canvas拥有多种的绘 ...
- 微信小程序 base64图片在canvas上画图
上代码 wxml <canvas canvas-id="myCanvas" style="width:400px;height:400px;">&l ...
- html5 canvas 自定义画图裁剪图片
html5 给我们带来了极大惊喜的canvas标签,有了它我们可以在浏览器客户端处理图片,不需要经过服务器周转.可以实现: 1.照片本地处理,ps有的一些基本功能都有 2.结合js可以实现一些很炫的动 ...
- canvas象棋 画图
今天写了一个canvas画图的象棋 .js基础不行,只画了个图,以后补充... <!DOCTYPE html> <html lang="en"> <h ...
- HTML Canvas 鼠标画图
原文来自:http://www.williammalone.com/articles/create-html5-canvas-javascript-drawing-app(已被墙) 译文: http: ...
- canvas防画图工具
<style> body { background: black; text-align: center; } #cans { background: white; } < ...
- html5 canvas 实现简单的画图
今天早上看了一下 canvas 前端画图,数据可视化, 百度的 echart.js , d3等 js 库都已经提供了强大的绘制各种图形的 API. 下面记录一下 有关canvas 绘图的基本知识: ...
- 兼容小程序的canvas画图组件jmGraph
基于CANVAS的简单画图组件让你用类似于dom的方式,在canvas上画图,感觉会不会很爽. 主页:http://graph.jm47.com/示例:http://graph.jm47.com/ex ...
随机推荐
- awk统计nginx日志访问前一百的ip
访问ip awk '{print $1}' access.log| sort | uniq -c | sort -n -k 1 -r | head -n 100 访问地址 awk '{print $ ...
- Object的属性property详细解释(自动生成成员变量)
类Class中的属性property: 在ios第一版中,我们为输出口同时声明了属性和底层实例变量,那时,属性是oc语言的一个新的机制,并且要求你必须声明与之对应的实例变量,例如: @interfac ...
- PM2的使用
PM2 是一个带有负载均衡功能的 Node 应用的进程管理器. 安装 npm install -g pm2 启动程序:pm2 start <app_name|id|all> 列举进程:pm ...
- GIS 学习及参考站点
地理信息论坛 GIS空间站 GISALL 广东水利厅 flex版的
- 如何查看mysql版本
查到大概有5种,5.6.20就是版本号 1:在终端下:mysql -V. 以下是代码片段: 2:在mysql中:mysql> status;以下是代码片段: 3:在help里面查找,以下是代码片 ...
- JQuery:JQuery语法、选择器、事件处理
JQuery语法: 通过 jQuery,您可以选取(查询,query) HTML 元素,并对它们执行"操作"(actions). 一.语法:jQuery 语法是通过选取 HTM ...
- 3d sphere opengl
http://stackoverflow.com/questions/5988686/creating-a-3d-sphere-in-opengl-using-visual-c
- 四元数(Quaternion)和旋转(转)
http://blog.csdn.net/candycat1992/article/details/41254799 四元数介绍 旋转,应该是三种坐标变换--缩放.旋转和平移,中最复杂的一种了.大家应 ...
- Android Mina框架的学习笔记
Apache MINA(Multipurpose Infrastructure for Network Applications) 是 Apache 组织一个较新的项目,它为开发高性能和高可用性的网络 ...
- windows下制作PHP扩展
一.编译PHP 转自:http://demon.tw/software/compile-php-on-windows.html 编译PHP扩展必需的一些头文件需要从php源码中获取,其中有一些配置性的 ...