<html lang="en">
<head>
<meta charset="utf-8">
<title>JS Bin</title>
<style>
#myCanvas {
border: 1px solid black;
}
</style>
<script>
var canvas, ctx;
// List of images to load, we used a JavaScript object instead of
// an array, so that named indexes (aka properties)
// can be used -> easier to manipulate
var imagesToLoad = {
flowers: 'http://cdn2.digitalartsonline.co.uk/images/features/1675/intro.jpg',
lion: 'http://farm3.static.flickr.com/2319/2486384418_8c031fec76_o.jpg',
blackAndWhiteLys: 'http://pshero.com/assets/tutorials/0062/final.jpg',
tiledFloor: 'http://4.bp.blogspot.com/_Rqs7w7m37B4/TETj5rD_QmI/AAAAAAAAADk/qRiwoTO-zKk/s1600/symmetry:assymetry+repeatable+pattern.jpg'
};
function loadImages(imagesToBeLoaded, drawCallback) {
var imagesLoaded = {};
var loadedImages = 0;
var numberOfImagesToLoad = 0;
// get num of sources
for(var name in imagesToBeLoaded) {
numberOfImagesToLoad++;
}
for(var name in imagesToBeLoaded) {
imagesLoaded[name] = new Image();
imagesLoaded[name].onload = function() {
if(++loadedImages >= numberOfImagesToLoad) {
drawCallback(imagesLoaded);
} // if
}; // function
imagesLoaded[name].src = imagesToBeLoaded[name];
} // for
} // function
function init() {
// This function is called after the page is loaded
// 1 - Get the canvas
canvas = document.getElementById('myCanvas');
// 2 - Get the context
ctx=canvas.getContext('2d');
loadImages(imagesToLoad, function(imagesLoaded) {
patternFlowers = ctx.createPattern(imagesLoaded.flowers, 'repeat');
patternLion = ctx.createPattern(imagesLoaded.lion, 'repeat');
patternBW = ctx.createPattern(imagesLoaded.blackAndWhiteLys, 'repeat');
patternFloor = ctx.createPattern(imagesLoaded.tiledFloor, 'repeat');
drawRectanglesWithPatterns();
});
}
function drawRectanglesWithPatterns() {
ctx.fillStyle=patternFloor;
ctx.fillRect(0,0,200,200);
ctx.fillStyle=patternLion;
ctx.fillRect(200,0,200,200);
ctx.fillStyle=patternFlowers;
ctx.fillRect(0,200,200,200);
ctx.fillStyle=patternBW;
ctx.fillRect(200,200,200,200);
}
</script>
</head>
<body onload="init();">
<canvas id="myCanvas" width="400" height="400">
Your browser does not support the canvas tag.
</canvas>
</body>
</html>
Output
300px
You can jump to the latest bin by adding /latest
to your URL
Keyboard Shortcuts
Shortcut | Action |
---|---|
ctrl + [num] | Toggle nth panel |
ctrl + 0 | Close focused panel |
ctrl + enter | Re-render output. If console visible: run JS in console |
Ctrl + l | Clear the console |
ctrl + / | Toggle comment on selected lines |
ctrl + ] | Indents selected lines |
ctrl + [ | Unindents selected lines |
tab | Code complete & Emmet expand |
ctrl + shift + L | Beautify code in active panel |
ctrl + s | Save & lock current Bin from further changes |
ctrl + shift + s | Open the share options |
ctrl + y | Archive Bin |
Complete list of JS Bin shortcuts |
JS Bin URLs
URL | Action |
---|---|
/ | Show the full rendered output. This content will update in real time as it's updated from the /edit url. |
/edit | Edit the current bin |
/watch | Follow a Code Casting session |
/embed | Create an embeddable version of the bin |
/latest | Load the very latest bin (/latest goes in place of the revision) |
/[username]/last | View the last edited bin for this user |
/[username]/last/edit | Edit the last edited bin for this user |
/[username]/last/watch | Follow the Code Casting session for the latest bin for this user |
/quiet | Remove analytics and edit button from rendered output |
.js | Load only the JavaScript for a bin |
.css | Load only the CSS for a bin |
Except for username prefixed urls, the url may start with http://jsbin.com/abc and the url fragments can be added to the url to view it differently. |