<!DOCTYPE html>
<html>
<body>
<canvas id ='output' width='150' height='150'></canvas>
<script>
var aCanvas = document.getElementById('output');
var ctx = aCanvas.getContext('2d');
var patternCanvas = document.createElement('canvas');
patternCanvas.width = 30;
patternCanvas.height = 30;
var patternCtx = patternCanvas.getContext('2d');
patternCtx.fillStyle = '#f00';
patternCtx.fillRect(0, 0, 15, 15);
patternCtx.fillStyle = '#0f0';
patternCtx.fillRect(15, 0, 15, 15);
patternCtx.fillStyle = '#00f';
patternCtx.fillRect(0, 15, 15, 15);
patternCtx.fillStyle = "#ff0";
patternCtx.fillRect(15, 15, 15, 15);
var myPattern = ctx.createPattern(patternCanvas, 'repeat');
ctx.fillStyle = myPattern;
ctx.fillRect(0, 0, aCanvas.width, aCanvas.height);
</script>
</body>
</html>