最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

html - Redrawing Canvas in HTML5 using Javascript - Stack Overflow

programmeradmin1浏览0评论

I am creating a click and clear game. Once the user clicks some brick its adjacent bricks are checked for same color and all these bricks are bricks are cleared at once.

These are Cleared using clearRect() function.

Now this leaves a white patch right between the bricks above and bricks below leaving the above bricks hanging.

Now i want to bring these bricks above downward. How do i do this..? Plz help

I am creating a click and clear game. Once the user clicks some brick its adjacent bricks are checked for same color and all these bricks are bricks are cleared at once.

These are Cleared using clearRect() function.

Now this leaves a white patch right between the bricks above and bricks below leaving the above bricks hanging.

Now i want to bring these bricks above downward. How do i do this..? Plz help

Share Improve this question edited Aug 2, 2012 at 9:04 Ganesh Somani asked Aug 2, 2012 at 8:37 Ganesh SomaniGanesh Somani 2,3602 gold badges28 silver badges38 bronze badges 3
  • please mention ur question more clear. If possible post sample code. – Praveen Singh Commented Aug 2, 2012 at 8:39
  • I want to create a game like this but after clicking my bricks remain in air. I want to shift them down wards – Ganesh Somani Commented Aug 2, 2012 at 8:48
  • MarcoK answers your question here perfectly for how to redraw the canvas. I suggest starting another question with much more detail and your current code examples on regarding getting your "bricks" to fall. – Sphvn Commented Aug 2, 2012 at 9:00
Add a ment  | 

3 Answers 3

Reset to default 11

The question is quite vague, but based on the title, you'll need to clear your canvas before you can redraw. Otherwise, the drawn elements would simply stack on top of each other.

To do this, call the function clearRect on the canvas itself:

function clear() {
    var canvas = document.getElementById("canvas");
    var ctx = canvas.getContext("2d");
    ctx.clearRect(0, 0, 500, 500);
}

Where canvas is the ID of your canvas, and 500, 500 the dimensions. Now you'll have an empty canvas where you can redraw everything again.

I once created a simple HTML5 canvas game as well, you might learn from the source code.

I think I understand what you're asking. If so then you're wanting to know how to move the blocks down when the blocks below have been removed.

This is just a matter of increasing the x position (remember the canvas starts at 0,0) with each iteration of your game loop.

How far to increase? Well that would be to where the highest "solid tower" is. I.E., say you have a column of 10 tokens and you remove the 7. The 3 below need all fall to the height of the remaining 6 - so that would be board height - (6*token height)

*
*
*
+ <- remove
* <- 6x token height (and less the board height)
*  
*  
*
*
*

I had success at redrawing the HTML Canvas by DOM.

    var c = document.getElementsByName("myCanvas")[0];

    if (c != null)
    {
        c.remove();
    }

    var c = document.createElement("canvas");
    c.setAttribute("name", "myCanvas");
    c.setAttribute("width", 900);
    c.setAttribute("height", 600);
    c.setAttribute("style", "border:1px solid #d3d3d3");
发布评论

评论列表(0)

  1. 暂无评论