官术网_书友最值得收藏!

Chapter 4. Building the Untangle Game with Canvas and the Drawing API

One new highlighted feature in HTML5 is the canvas element. We can treat it as a dynamic area where we can draw graphics and shapes with scripts.

Images in websites have been static for years. There are animated GIFs, but they cannot interact with visitors. Canvas is dynamic. We draw and modify the context in the Canvas, dynamically through the JavaScript drawing API. We can also add interaction to the Canvas and thus make games.

In the previous two chapters, we discussed DOM-based game development with CSS3 and a few HTML5 features. In the coming two chapters, we will focus on using new HTML5 features to create games. In this chapter, we will take a look at a core feature, Canvas, and some basic drawing techniques.

In this chapter, we will cover the following topics:

  • Introducing the HTML5 canvas element
  • Drawing a circle in Canvas
  • Drawing lines in the canvas element
  • Interacting with drawn objects in Canvas with mouse events
  • Detecting a line intersection
  • Supporting the drag-n-drop feature in touch devices

The Untangle puzzle game is a game where players are given circles with some lines connecting them. The lines may intersect the others and the players need to drag the circles so that no line intersects anymore.

The following screenshot previews the game that we are going to achieve through this chapter:

You can also try the game at the following URL:

http://makzan.net/html5-games/untangle-wip-dragging/

So let's start making our Canvas game from scratch.

主站蜘蛛池模板: 长寿区| 孙吴县| 上饶县| 广宗县| 五台县| 和平区| 永川市| 元江| 冕宁县| 锡林浩特市| 财经| 舒兰市| 遂宁市| 化州市| 保德县| 广丰县| 临清市| 隆安县| 横峰县| 南澳县| 马龙县| 贡嘎县| 措美县| 焦作市| 乐安县| 灯塔市| 从江县| 东乌珠穆沁旗| 衢州市| 康马县| 土默特右旗| 育儿| 清水河县| 当雄县| 沭阳县| 汉中市| 静安区| 长顺县| 锡林浩特市| 胶州市| 滦南县|