惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

腾讯CDC
GbyAI
GbyAI
C
CERT Recently Published Vulnerability Notes
S
Security @ Cisco Blogs
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
The Hacker News
The Hacker News
D
Darknet – Hacking Tools, Hacker News & Cyber Security
P
Proofpoint News Feed
C
Cyber Attacks, Cyber Crime and Cyber Security
S
Securelist
Security Latest
Security Latest
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
Simon Willison's Weblog
Simon Willison's Weblog
Latest news
Latest news
T
Tor Project blog
T
Threat Research - Cisco Blogs
Recent Commits to openclaw:main
Recent Commits to openclaw:main
Spread Privacy
Spread Privacy
K
Kaspersky official blog
T
The Exploit Database - CXSecurity.com
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
V2EX - 技术
V2EX - 技术
L
Lohrmann on Cybersecurity
Google Online Security Blog
Google Online Security Blog
Cyberwarzone
Cyberwarzone
Help Net Security
Help Net Security
The Last Watchdog
The Last Watchdog
C
Cybersecurity and Infrastructure Security Agency CISA
Attack and Defense Labs
Attack and Defense Labs
大猫的无限游戏
大猫的无限游戏
Schneier on Security
Schneier on Security
H
Heimdal Security Blog
O
OpenAI News
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
V
Visual Studio Blog
AI
AI
H
Hacker News: Front Page
博客园_首页
博客园 - 【当耐特】
L
LINUX DO - 最新话题
MyScale Blog
MyScale Blog
量子位
Vercel News
Vercel News
C
Cisco Blogs
L
LINUX DO - 热门话题
Y
Y Combinator Blog
T
Threatpost
爱范儿
爱范儿
P
Privacy & Cybersecurity Law Blog

博客园 - 君之蘭

WPF打印票据 easyui plugin——etreegrid:CRUD Treegrid 【经验谈】XmlSerializer的坑 从抽象谈起(三):AOP编程和ASP.NET MVC 从抽象谈起(二):观察者模式与回调 从抽象谈起(一):工厂模式与策略模式 SQL SERVER BI 入门:(2) Analysis Service 应用 SQL SERVER BI 入门:(1)安装与基础概念 HTML5 Canvas编写五彩连珠(6):试玩 HTML5 Canvas编写五彩连珠(5):寻路 HTML5 Canvas编写五彩连珠(4):动画 HTML5 Canvas编写五彩连珠(3):设计 HTML5 Canvas编写五彩连珠(1):预览 Trie树-脏词过滤应用 带你走进缓存世界(6):共享缓存 ASP.NET MVC3 Custom FormAuthorize ASP.NET MVC3 Custom ErrorPages 500/404 带你走进缓存世界(5):一显身手 带你走进缓存世界(4):缓存之缓
HTML5 Canvas编写五彩连珠(2):画图
君之蘭 · 2012-03-11 · via 博客园 - 君之蘭

好吧,新的一天来了,我才开始动笔,真够懒得:)昨天说了今天我们要画一个球,在canvas上。好吧,这是游戏的入门的第一步,只是昨天没写完,所以。。。

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
</head>
<body>
    <canvas id="canvas" height="400" width="600" style="background: #fff;"></canvas>
    <script type="text/javascript">
        var canvas = document.getElementById("canvas");
        var ctx = canvas.getContext("2d");
        ctx.beginPath();

        ctx.arc(110, 110, 40, 0, Math.PI * 2);

        ctx.strokeStyle = "red";
        ctx.fillStyle = "yellow";

        ctx.fill();
        ctx.stroke();

    </script>
</body>
</html>

  上面的代码是在VS11 beta上写的,实在是太舒服了,vs是非常强大的编辑器。上面的代码中我们绘制了一个大大的圆,并且着色了,红边和黄心。
看下 arc (弧度)方法,昨天的文章里有他的链接地址,我在这里粘贴下。
 The arc(xyradiusstartAngleendAngleanticlockwise) method draws an arc. 
arc(x,y,弧度,开始角度点,结束角度点, 顺时针),角度点你可能不是很清楚如何表示,这里是用Math.PI 圆周率来表示 6.28是周长。 想画圆的一部分也就是一段弧线,可以取开始的角度点和结束的角度点。

   那么下一步就是要把圆能够画到我们棋盘上。其实,这个很简单,只要我们把x,y和radius的值调整下就会绘制出来。我把昨天代码写的更“专业”了一点。所以,今天的代码会在新的代码基础上增加了。先看下改动过的代码。

        var canvas = document.getElementById("canvas");
        var ctx = canvas.getContext("2d");
        var g = {
            cellCount: 9,
            lineCount: 5,
        };

        var map = {
            startX: 20.5,
            startY: 60.5,
            cellWidth: 30,
            getEndX: function () {
                return g.cellCount * this.cellWidth + this.startX;
            },
            getEndY: function () {
                return g.cellCount * this.cellWidth + this.startY;
            },
            draw: function () {
                ctx.beginPath();

                ctx.moveTo(this.startX, this.startY);

                for (var i = 0; i <= g.cellCount; i++) {

                    var p1 = i * this.cellWidth + this.startX;
                    ctx.moveTo(p1, this.startY);
                    ctx.lineTo(p1, this.getEndY());

                    var p2 = i * this.cellWidth + this.startY;
                    ctx.moveTo(this.startX, p2);
                    ctx.lineTo(this.getEndX(), p2);

                }
                ctx.strokeStyle = "#456";
                ctx.stroke();
            },

        };

        map.draw();

是吧,更专业了吧,这样就不会定义一坨的function,到时候没出找,而是定义在一个对象里,这种封装也能避免命名冲突。而且,棋盘起始的位置我也做了调整,只要修改起始的x y值,棋盘就会在这个点开始画。  那,现在我们要在第五行,第六列画一个黄色的球,该怎么画呢?只需要给map增加一个方法,再调用这个方法就行啦

            drawBubble: function (x, y) {
                var px = this.startX + this.cellWidth * x - this.cellWidth / 2;
                var py = this.startY + this.cellWidth * y - this.cellWidth / 2;
                ctx.beginPath();
                ctx.arc(px, py, 12, 0, Math.PI * 2);
                ctx.strokeStyle = "white";
                ctx.fillStyle = "yellow";
                ctx.fill();
                ctx.stroke();
            },

画出来刷新下,居然是第六行,第五列,我们搞错了吗?代码没有,只是我们误认为x是行y是列 按顺序叫顺口了,其实是反过来的:)

泡泡既然能画出来,也要能清除才是,不然没法玩,所以再增加一个清除泡泡的方法。
 

            clearBubble: function (x, y) {
                var px = this.startX + this.cellWidth * x - this.cellWidth + 0.5;
                var py = this.startY + this.cellWidth * y - this.cellWidth + 0.5;
                ctx.beginPath();
                ctx.clearRect(px, py, this.cellWidth - 1, this.cellWidth - 1);
                ctx.stroke();
            }

 ok,是不是很霸气? o(∩_∩)o 哈哈,不过在获取泡泡的位置时是不是很纠结,为什么画泡泡是 width/2 而擦除要加0.5? 
画圆是从中心点开始画,所以要去掉半径,而擦数不能擦掉我们的棋盘线,所以要偏移0.5 。