Placing content on canvas, is it posible?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ilya Kraft
    New Member
    • Jan 2011
    • 134

    Placing content on canvas, is it posible?

    Hello, i found this cool HTML5 feature called Gradient canvas. I would like to use it as a website background, is it possible to somehow place content on it? when i use divs and table nothing appears only this canvas.

    Here is a file i want to use as a background.

    Code:
    <!DOCTYPE html>
    <html lang="en"><head>
    <meta charset="utf-8">
    <title>ilya daderko</title>
    <style>
    body {
      background: #000;
    }
    canvas {
      position: absolute;
      top: 0;
      left: 0;
      height: 100%;
      width: 100%;
      background-repeat:repeat
    }
    
    body,td,th {
    	color: #FFF;
    }
    </style>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    </head>
    <body>
    <canvas height="600" width="600">
    </canvas>
    <script>
    var canvas = document.getElementsByTagName('canvas')[0],
        ctx = null,
        grad = null,
        body = document.getElementsByTagName('body')[0],
        color = 255;
        
    if (canvas.getContext('2d')) {
      ctx = canvas.getContext('2d');
      ctx.clearRect(0, 0, 600, 600);
      ctx.save();
      // Create radial gradient
      grad = ctx.createRadialGradient(0,0,0,0,0,600); 
      grad.addColorStop(0, '#000');
      grad.addColorStop(1, 'rgb(' + color + ', ' + color + ', ' + color + ')');
    
      // assign gradients to fill
      ctx.fillStyle = grad;
    
      // draw 600x600 fill
      ctx.fillRect(0,0,600,600);
      ctx.save();
      
      body.onmousemove = function (event) {
        var width = window.innerWidth, 
            height = window.innerHeight, 
            x = event.clientX, 
            y = event.clientY,
            rx = 600 * x / width,
            ry = 600 * y / width;
            
        var xc = ~~(256 * x / width);
        var yc = ~~(256 * y / height);
    
        grad = ctx.createRadialGradient(rx, ry, 0, rx, ry, 0.01); 
        grad.addColorStop(0, '#000');
        grad.addColorStop(1, ['rgb(', xc, ', ', (255 - xc), ', ', yc, ')'].join(''));
        // ctx.restore();
        ctx.fillStyle = grad;
        ctx.fillRect(0,0,600,600);
        // ctx.save();
      };
    }
    </script>
    <a><img style="position: absolute; top: 0; left: 0; border: 0;" src="website/images/Logotipe2.png" alt="Fork me on GitHub"></a>
    
    try {
    var pageTracker = _gat._getTracker("UA-1656750-18");
    pageTracker._trackPageview();
    } catch(err) {}</script>
    
    
    
    
    </body></html>
  • Rabbit
    Recognized Expert MVP
    • Jan 2007
    • 12517

    #2
    You could probably overlap it with absolute postitioning but that's a lot of work... why not just screenshot it and make a bitmap from it?

    Comment

    • ilya Kraft
      New Member
      • Jan 2011
      • 134

      #3
      yeh, but if I bitmap it it would not change colour would it? I'm kinda new to html, i kno basics do you have any advice on overlaping it?

      Comment

      • ilya Kraft
        New Member
        • Jan 2011
        • 134

        #4
        this one is interactive, when i move mouse it changes colours ))

        Comment

        • Rabbit
          Recognized Expert MVP
          • Jan 2007
          • 12517

          #5
          This site has a tutorial on CSS and positioning. You'll probably want to wrap the rest of the content in an absolute positioned div. http://www.w3schools.com/css/css_positioning.asp

          Comment

          • ilya Kraft
            New Member
            • Jan 2011
            • 134

            #6
            It seems to work, thank you very much )))

            Comment

            • Rabbit
              Recognized Expert MVP
              • Jan 2007
              • 12517

              #7
              No problem, good luck.

              Comment

              Working...