+ Reply to Thread
Results 1 to 7 of 7

Thread: Interesting effect with the canvas element and CSS

  1. #1
    sales.satanicspider74 is offline x10Hosting Member sales.satanicspider74 is an unknown quantity at this point
    Join Date
    Mar 2011
    Posts
    5

    Interesting effect with the canvas element and CSS

    So I have a page with 2 canvas elements.

    Used JS to draw a 10x10 rectangle to each canvas. And they looked correct.

    Then resized the canvas elements with a css class using the width and height properties. Instead of pixles I used percentage. First canvas width 100% height 88%. Second canvas width 100% height 8%.

    Without changing the js the squares are not the same size.


    Weird eh?

  2. #2
    misson is offline x10 Spammer misson is a jewel in the rough
    Join Date
    Mar 2008
    Location
    Libertatia
    Posts
    2,506

    Re: Interesting effect with the canvas element and CSS

    Without sample code, your question is imperfect. The problem description is too vague to address.
    Be sure to read all pages linked in this post; they have further information that should prove useful. When asking for help, make sure you follow Eric Raymond's and Jon Skeet's guidelines for prompt, accurate responses. Please answer any questions I ask; they're not rhetorical (probably). Any posted code is intended as illustrative example, rather than a solution to your problem to be copied without alteration. Study it to learn how to write your own solution.
    Misson, not Mission.

  3. #3
    lemon-tree's Avatar
    lemon-tree is offline x10 Minion lemon-tree has a spectacular aura about
    Join Date
    Nov 2007
    Posts
    1,420

    Re: Interesting effect with the canvas element and CSS

    This is because the canvas element only redraws itself when you specifically give it an instruction through JS and between these such requests it can be likened to a simple image element, so it behaves just is resizing an image would. This is done so that you can alter the canvas through simple CSS resizing and rotating etc without having to go back to the JS and redrawing it, thus making development an awful lot more efficient.

  4. #4
    sales.satanicspider74 is offline x10Hosting Member sales.satanicspider74 is an unknown quantity at this point
    Join Date
    Mar 2011
    Posts
    5

    Re: Interesting effect with the canvas element and CSS

    Been a while been busy ...

    From my css file
    Code:
    canvas.main
    {
    	width:100%;
    	height:40%;
    }
    the canvas element
    Code:
    <canvas id="main" class="main" onClick="javascript:draw()">
    This text is displayed if your browser does not support HTML5 Canvas.
    </canvas>
    And the script. Draw a red and blue rectangle with a width and height of 10 aka a square

    Code:
    function draw()
    {
    	var example = document.getElementById('main');
    	var context = example.getContext('2d');
    	context.fillStyle = "rgb(255,0,0)";
    	context.fillRect(0, 0, 10, 10);
    	context.fillStyle = "rgb(0,0,255)";
    	context.fillRect(20, 20, 10, 10);
    }
    Result -- 2 rectangles not squares

    If i change the canvas size to 100% 100% they are squares ... although not 10 px each. Tested in chrome and firefox

  5. #5
    denzil is offline x10 Sophmore denzil is an unknown quantity at this point
    Join Date
    Jan 2011
    Location
    South Africa
    Posts
    134

    Re: Interesting effect with the canvas element and CSS

    I'm quite sure lemon-tree gave an accurate description of what's going on right above your reply!!

  6. #6
    sales.satanicspider74 is offline x10Hosting Member sales.satanicspider74 is an unknown quantity at this point
    Join Date
    Mar 2011
    Posts
    5

    Re: Interesting effect with the canvas element and CSS

    The way he had stated it i if the canvas were drawn on THEN re-sized it would do that. I have the canvas sized then drawing on it.

    By the way he had stated it:
    Draw a 10x10 square
    Resize canvas to 1/2 height or so
    Square is now a rectangle that is 5x10

    What I am pointing out:
    Size a canvas to 1/2 height or so
    Draw a 10x10 square
    Square should still be 10x10 but is not

  7. #7
    descalzo's Avatar
    descalzo is offline Grim Squeaker descalzo has a brilliant futuredescalzo has a brilliant futuredescalzo has a brilliant future
    Join Date
    Jul 2009
    Location
    Ankh-Morpork
    Posts
    7,636

    Re: Interesting effect with the canvas element and CSS

    I get what you describe in Chrome, but not in FireFox. Firefox renders squares. Interestingly, the blue square has fuzzy edges and the bottom & right edges of the red square are fuzzy in Firefox.

    If I resize the Chrome window, the images change proportions in the direction of the resize ... I can make the images square.

    If I resize the Firefox window, the images change absolute size, but remain squares
    Last edited by descalzo; 04-27-2011 at 04:30 PM.
    Nothing is always absolutely so.

+ Reply to Thread

Similar Threads

  1. XML Parsing Error: no element found
    By joennika214 in forum Programming Help
    Replies: 0
    Last Post: 04-11-2010, 09:34 PM
  2. [MySQL] Getting 1st element of list
    By Markstar in forum Programming Help
    Replies: 6
    Last Post: 01-29-2010, 04:37 AM
  3. Replies: 0
    Last Post: 11-23-2006, 03:11 PM
  4. canvas size problem
    By NewFuture in forum Scripts & 3rd Party Apps
    Replies: 3
    Last Post: 07-23-2005, 08:20 AM
  5. New Element In The Periodic Table
    By ironcross77 in forum Off Topic
    Replies: 1
    Last Post: 06-28-2005, 01:02 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
x10hosting free hosting for the masses
dedicated servers