I would highly recommend not using a scaled image if you are aiming for sharp and solid results with a background that repeats over time. Here's a good, 100% software way to do this with JS and Canvas. The entire JS block, as described here, takes about half a millisecond to complete.
Here jsFiddle
var c = document.getElementById("myCanvas"); var ctx = c.getContext("2d"); var w = c.getAttribute('width'); var h = c.getAttribute('height'); var color = c.getAttribute('data-color'); ctx.rect(-1, -1, ++w, ++h); ctx.lineWidth = 1; ctx.strokeStyle = color; ctx.stroke(); var body = document.getElementsByTagName('body')[0]; body.style.backgroundImage = 'url(' + c.toDataURL("image/png") + ')';
And HTML is simple:
<canvas id="myCanvas" width="20" height="20" data-color="#666666" style="display:none">Your browser does not support the HTML5 canvas tag.</canvas>
All you have to do is echo the width and height that you do not use in this canvas tag (it is best to use the server-side technology that you use) and everything will be done automatically .
Let me explain this: ctx.rect(-1, -1, ++w, ++h); We take the required width and height and put it in variables, then when drawing our rectangle we want the width to start with [-1, -1] coordinates, so we donβt have the left and top borders painted (we donβt need this if we want our pattern to be uneven), and ++w and ++h simply increase the width and height of the colored rectangle by one pixel to go beyond the canvas and thus separate these two non-zero borders.
Try changing the width and height in the `canvas` tag in the script I made and see if you like the output.
By going this way, you can also control the line width as well as the color.
source share