canvas

canvas元素

letcanvasDOM =document.getElementById('box');letcav = canvasDOM.getContext('2d');复制代码

绘制线段

// 线段,两点一线// 设置起点cav.moveTo(150,100);// 设置终点cav.lineTo(300,100);// 设置线段颜色cav.strokeStyle ='skyblue';// 设置线段宽度cav.lineWidth ='8'// 绘制线段cav.stroke()复制代码

通过stroke方法构建画板

canvasDOM.onmousedown =function(event){letevent = e ||window.eventvarx = event.clientX -this.offsetLeft -parseInt(this.style.borderLeftWidth)vary = event.clientY -this.offsetTop -parseInt(this.style.borderTopWidth)    cav.moveTo(x, y);    canvasDOM.onmousemove =function(event){varx2 = event.clientX -this.offsetLeft -parseInt(this.style.borderLeftWidth)vary2 = event.clientY -this.offsetTop -parseInt(this.style.borderTopWidth)        cavs.lineTo(x2, y2)        cav.stroke()    }    canvasDOM.onmouseup =function(event){        canvasDOM.onmousemove =null}}复制代码

绘制路径

cav.beginPath();cav.moveTo(200,100);cav.lineTo(200,200);cav.lineTo(300,200);cav.closePath();cav.strokeStyle ='skyblue';cav.lineWidth ='5';cav.fillStyle ='pink';cav.fill()cav.stroke()复制代码

绘制矩形

cav.fillStyle ='skyblue'// 绘制填充矩形// fillRect(x,y,width,height)cav.fillRect(100,100,300,200)cav.strokeStyle ='pink'cav.lineWidth =8// 绘制矩形边框cav.strokeRect(100,100,300,200)// clearRect 在给定的矩形内清除指定的像素cav.clearRect(125,125,100,100)

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 1.利用canvas 写一个 牛 字: <!-- 宽高必须写在元素本身 canvas id="canvasId" ...
    GGYY丶阅读 2,880评论 0 0
  • let canvasDom = document.getElementById('canvasId'); ...
    过河卒_51be阅读 1,382评论 0 0
  • let canvasDom = document.getElementById('canvasId'); ...
    冲锋敢死曾小贤阅读 1,603评论 0 0
  • 在Canvas中,线段也是路径中的一种,被称之为线性路径。在Canvas中绘制线性路径主要用到moveTo(x,y...
    王叮叮当当响阅读 8,201评论 0 2
  • HTML5的canvas元素是HTML5技术标准中最令人振奋的功能之一。 它提供了一套强大的图形API,拥有多种绘...
    Zd_silent阅读 3,880评论 0 0

友情链接更多精彩内容