Canvas repeat and align problem, help pls.

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

    Canvas repeat and align problem, help pls.

    Hello I have this interactive canvas as a background of my web, it is on 100% width and high, but when I add content that continues longer than canvas and you have to scroll down, canvas is not repeating down y axis.

    Also can anyone tell how can i center align absolute positioned <div>.

    I added white box that is longer than canvas, if you scroll down you start to see black background, how can I make canvas repeat down y-axis and how do i center that white box (which is absolute positioned div)

    Here is code:

    Code:
    <!DOCTYPE html> 
    <html lang="en"> 
    <head> 
    <meta charset=utf-8 /> 
    <title>Canvas by Ilya</title> 
    <style> 
    body {
    	background: #000;
    	margin-left: 0px;
    	margin-top: 0px;
    	margin-right: 0px;
    }
    canvas {
      position: absolute;
      top: 0;
      left: 0;
      height: 100%;
      width: 100%;
      
    }
    body,td,th {
    	color: #000;
    }
    
    
    #repeattest {
    	background-color: #FFF;
    	position: absolute;
    	height: 2552px;
    	width: 88px;
    	left: 62px;
    	top: 1px;
    }
    </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>
    <div id="repeattest">
      <p>Scroll</p>
      <p> Down<br>
      </p>
    </div>
    
    
    </body> 
    </html>
  • Rabbit
    Recognized Expert MVP
    • Jan 2007
    • 12517

    #2
    Change the canvas from absolute to fixed. I'm not sure what you mean by center the div. Are you talking about centering what's inside the div? Or centering the div on the page?

    Comment

    • ilya Kraft
      New Member
      • Jan 2011
      • 134

      #3
      changing it to fixed position helped thanks, i mean centering a div on a page.
      Thanks for help with canvas again )))

      Comment

      • Rabbit
        Recognized Expert MVP
        • Jan 2007
        • 12517

        #4
        Have one master div that's absolutely positioned at 0,0 with 100% width and height. That will be the wrapper for the rest of the content. You can now treat everything within that div as if it's the <body> of the document. You would center anything within it the same way you would center it if you didn't have a canvas for a background or a div as a wrapper.

        Comment

        • ilya Kraft
          New Member
          • Jan 2011
          • 134

          #5
          Thank you again ))) you are awesome!!! ;D

          Comment

          • Rabbit
            Recognized Expert MVP
            • Jan 2007
            • 12517

            #6
            No problem, good luck.

            Comment

            Working...