JavaScript-Html-绘图-canvas基础

使用Html控件canvas,利用JavaScript自定义绘图

效果图如下:


<!DOCTYPE html>
<html>
    <body>
        <canvas id="canvas" width="300" height="350" style="border:4px solid #000;"/>
        <script>            
            var canvas = document.getElementById("canvas");
            var context = canvas.getContext("2d");

            // 1.画线段
            context.beginPath();
            context.moveTo(10,10);
            context.lineTo(280,10);
            context.lineWidth = 8;
            context.strokeStyle = "#00f";
            context.stroke();
            context.closePath();

            // 2.画圆框
            context.beginPath();
            context.arc(60,80,50,0,360,false);
            context.lineWidth = 4;
            context.strokeStyle = "#f00";
            context.stroke();
            context.closePath();        
            // 画实心圆
            context.beginPath();
            context.arc(60,180,50,0,360,false);
            context.fillStyle = "#00f";
            context.fill();
            context.closePath();

            // 3.画矩形框
            context.beginPath();
            context.strokeRect(150,40,100,50);
            context.closePath();            
            // 画实心矩形
            context.beginPath();
            context.fillRect(150,90,100,50);
            context.closePath();

            // 4.添加图片
            context.beginPath();
            var image = new Image();
            image.src = "http://avatar.csdn.net/5/3/C/1_qq_32115439.jpg";
            context.drawImage(image,150,160,100,100);
            context.closePath();

            // 5.添加文字
            context.beginPath();
            context.font = "60px 宋体";           
            context.fillText("lioil.win",10,320,280);
            context.closePath();
        </script>
    </body>
</html>

简书: http://www.jianshu.com/p/9e495ea9aea6
CSDN博客: http://blog.csdn.net/qq_32115439/article/details/72582063
GitHub博客:http://lioil.win/2017/05/20/JS_Html_Canvas.html
Coding博客:http://c.lioil.win/2017/05/20/JS_Html_Canvas.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值