<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. SQL Server批量数据导出导入BCP使用

    BCP简介 bcp是SQL Server中负责导入导出数据的一个命令行工具,它是基于DB-Library的,并且能以并行的方式高效地导入导出大批量的数据.bcp可以将数据库的表或视图直接导出,也能通过 ...

  2. C++ 字符串操作常见函数

    //字符串拷贝,排除指定字符 char *strcpy_exclude_char(char *dst, const int dst_len, const char *src, const char * ...

  3. Cocos2d-JS引入资源

    以图片为例: 创建项目后,把图片放入res文件夹,修改 app.js var HelloWorldLayer = cc.Layer.extend({ sprite:null, ctor:functio ...

  4. android动态调试samli代码(转)

    转载自看雪http://bbs.pediy.com/showthread.php?t=189610,非常感谢原作者分享! 跟踪apk一般的做法是在反编译的smali代码中插入log输出,然后重新编译运 ...

  5. The Beginner’s Guide to iptables, the Linux Firewall

    Iptables is an extremely flexible firewall utility built for Linux operating systems. Whether you’re ...

  6. Magento添加一个下拉登陆菜单Create Magento Dropdown Login in a few minutes

    Dropdown login forms are not a feature many online stores use, but in some cases they could be quite ...

  7. Educational Codeforces Round 16---部分题解

    710A. King Moves 给你图中一点求出它周围有几个可达的点: 除边界之外都是8个,边界处理一下即可: #include<iostream> #include<cstdio ...

  8. curl get post 数据

    1.get方式传值 function testGet(){ $ch = curl_init (); //初始化一个cURL会话 $url = "127.0.0.1/testPage?test ...

  9. ext4.1Grid中的column多选

    ext4.1中默认单选可以使用checkboxmodel实现多选selModel:Ext.create('Ext.selection.CheckboxModel'),

  10. SwipeRefreshLayout实现上拉加载下拉刷新

    package com.example.swiperefreshlayoutdemo; import java.util.ArrayList;import java.util.HashMap; imp ...