<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基本画图的更多相关文章

  1. canvas 在线画图

    canvas 在线画图 <!DOCTYPE html> <html lang="en"> <head> <meta charset=&qu ...

  2. 使用Canvas制作画图工具

      前  言 JRedu canvas是HTML5中重要的元素之一,canvas元素使用JavaScript在网页上绘制图像,画布是一个矩形区域,我们可以控制其每一个元素,并且canvas拥有多种的绘 ...

  3. 微信小程序 base64图片在canvas上画图

    上代码 wxml <canvas canvas-id="myCanvas" style="width:400px;height:400px;">&l ...

  4. html5 canvas 自定义画图裁剪图片

    html5 给我们带来了极大惊喜的canvas标签,有了它我们可以在浏览器客户端处理图片,不需要经过服务器周转.可以实现: 1.照片本地处理,ps有的一些基本功能都有 2.结合js可以实现一些很炫的动 ...

  5. canvas象棋 画图

    今天写了一个canvas画图的象棋 .js基础不行,只画了个图,以后补充... <!DOCTYPE html> <html lang="en"> <h ...

  6. HTML Canvas 鼠标画图

    原文来自:http://www.williammalone.com/articles/create-html5-canvas-javascript-drawing-app(已被墙) 译文: http: ...

  7. canvas防画图工具

    <style> body {   background: black;   text-align: center; } #cans {   background: white; } < ...

  8. html5 canvas 实现简单的画图

    今天早上看了一下 canvas 前端画图,数据可视化, 百度的 echart.js  , d3等 js 库都已经提供了强大的绘制各种图形的 API. 下面记录一下 有关canvas 绘图的基本知识: ...

  9. 兼容小程序的canvas画图组件jmGraph

    基于CANVAS的简单画图组件让你用类似于dom的方式,在canvas上画图,感觉会不会很爽. 主页:http://graph.jm47.com/示例:http://graph.jm47.com/ex ...

随机推荐

  1. 一种swift编码风格指南(供参考,by linkedin)

    http://www.cocoachina.com/swift/20160701/16894.html

  2. 安装CentOS Core之后布置环境脚本

    #启动ssh服务 service sshd start #安装vim编辑器 echo y | yum install vim #安装网络工具包 echo y | yum install net-too ...

  3. mssql全文索引

    在使用全文索引的时候例如: SELECT [PRID] ,[PRCode] ,[PRDesc] FROM [test1].[dbo].[PerformanceIssue] where contains ...

  4. ArcGIS Engine开发的ArcGIS 版本管理的功能

    原文:ArcGIS Engine开发的ArcGIS 版本管理的功能 转自:http://blog.csdn.net/linghe301/article/details/7965901 这是以前的Arc ...

  5. 修改OpenCart系统配置

    后台修改admin配置文件和修改根目录下的config.php <?php// HTTPdefine('HTTP_SERVER', 'http://网站域名/');define('HTTP_IM ...

  6. linux power button

    最近需要使用到power button按键,linux中有gpio keys的机制,只要注册即可. device注册 arch/arm/mach-mx6/board-mx6q_sabresd.c #d ...

  7. imx6 system boot

    imx6开机启动就进入download模式,有的板子进入文件系统之后会进入download模式.查看datasheet,Chapter 8 System Boot查找原因,记录于此. freescal ...

  8. java 中间件

    先说中间件:非底层操作系统软件.非业务应用软件,不是直接给最终用户使用的,不能直接给客户带来价值的软件,统称中间件.常见的有如下几种:服务中间件.集成中间件.数据中间件.消息中间件.安全中间件. 其中 ...

  9. dede如何实现二级栏目导航的仿制

    {dede:channelartlist row='2' typeid='1,2' }<h3><a href='{dede:field name='typeurl'/}'>{d ...

  10. JQuery:通过noConflict()方法同时使用jQuery 和其他框架

    jQuery - noConflict()方法 一.如何在页面上同时使用 jQuery 和其他框架?jQuery 和其他 JavaScript 框架正如您已经了解到的,jQuery 使用 $ 符号作为 ...