<html>
<head>
</head>
<body onload="init();">
<div id="canvasDiv">
<canvas id="myCanvas">
</canvas>
</div>
<div id="GUI">
<input id="colorselector" oninput="changeCouleur(this);" type="color"/>
<input id="selecteurEpaisseur" onchange="changeEpaisseur();" type="range" min="1" max="20"/>
<div class="color" id="red"></div>
<div class="color" id="blue"></div>
<div class="color" id="green"></div>
<div class="color" id="yellow"></div>
<div class="color" id="black"></div>
<div class="color" id="white"></div>
</div>
</body>
</html>
.color {
border: 2px solid black;
width : 40px;
height : 40px;
float:left;
}
#red {
background-color: red;
}
#green {
background-color: green;
}
#blue {
background-color: blue;
}
#black {
background-color: black;
}
#white {
background-color: white;
}
#yellow {
background-color: yellow;
}
@media all and (orientation: landscape) {
#myCanvas{
background-color: red;
//width:78%;
//height:100%;
//margin-left:142px;
}
#canvasDiv {
position : absolute;
top : 0;
left : 0;
border-right : 1px solid #aaaaaa;
border-bottom : 1px solid #aaaaaa;
border-bottom-right-radius : 20px;
cursor : crosshair;
margin-left:142px;
width:100%
}
#GUI {
float:left;
width: 142px;
top:1px;
height:100%;
position:fixed;
}
}
@media all and (orientation: portrait) {
#myCanvas{
background-color: green;
//width:100%;
//height:78%;
float:left;
}
#GUI {
clear:both;
top: 73.7%;
height:26%;
width:100%;
position:fixed;
}
}
var currentPos, previousPos;
var canvas, context, started;
function writeMessage(canvas, message) {
var context = canvas.getContext('2d');
context.clearRect(0, 0, canvas.width, canvas.height);
context.font = '18pt Calibri';
context.fillStyle = 'black';
context.fillText(message, 10, 25);
}
function getMousePos(canvas, evt) {
// get canvas position
var obj = canvas;
var top = 0;
var left = 0;
while (obj && obj.tagName != 'BODY') {
top += obj.offsetTop;
left += obj.offsetLeft;
obj = obj.offsetParent;
}
// return relative mouse position
var mouseX = evt.clientX - left + window.pageXOffset;
var mouseY = evt.clientY - top + window.pageYOffset;
return {
x:mouseX,
y:mouseY
};
}
function getTouchPos(canvas, evt) {
// get canvas position
var obj = canvas;
var top = 0;
var left = 0;
while (obj && obj.tagName != 'BODY') {
top += obj.offsetTop;
left += obj.offsetLeft;
obj = obj.offsetParent;
}
// return relative touch position
var touchX = evt.touches[0].clientX - left + window.pageXOffset;
var touchY = evt.touches[0].clientY - top + window.pageYOffset;
return {
x:touchX,
y:touchY
};
}
function clicked(evt) {
previousPos = getMousePos(canvas, evt);
started = true;
}
function released(evt) {
started = false;
}
function drawLine(previousPos, currentPos) {
context.beginPath();
context.moveTo(previousPos.x, previousPos.y);
context.lineTo(currentPos.x, currentPos.y);
context.stroke();
}
function init () {
canvas = document.getElementById('myCanvas');
context = canvas.getContext('2d');
started = false, previousPos;
canvas.addEventListener('mousedown', clicked);
canvas.addEventListener('mouseup', released);
canvas.addEventListener('mouseout', function() {
console.log("mouse out");
released();
});
// On récupère l'épaisseur courante
changeEpaisseur();
canvas.addEventListener('mousemove', function (evt) {
currentPos = getMousePos(canvas, evt);
var message = "Mouse position: " + currentPos.x + "," + currentPos.y;
// TRY TO UNCOMMENT THIS LINE !
//writeMessage(canvas, message);
// Let's draw some lines that follow the mouse pos
if (started) {
drawLine(previousPos, currentPos);
previousPos = currentPos;
}
}, false);
canvas.addEventListener("touchstart", function(event) {
previousPos = getTouchPos(canvas, event);
started = true;
});
canvas.addEventListener("touchend", function(event) {
released();
console.log("touchend");
});
canvas.addEventListener("touchmove", function(event) {
event.preventDefault()
currentPos = getTouchPos(canvas, event);
console.log("touchmove");
if (started) {
drawLine(previousPos, currentPos);
previousPos = currentPos;
}
});
// Ajoute des écouteurs de click sur les divs contenant les couleurs
var listeDivsCouleurs = document.querySelectorAll(".color");
// On récupère une liste de Noeuds DOM, on va ajouter un écouteur sur chacun
ajouteEcouteurSurDivCouleurs(listeDivsCouleurs);
};
/* Cette fonction ajoute un écouteur de click sur chaque div permettant
un choix de couleur. On écoute juste l'évenement "click" car un touchstart
sur cet type d'objet génère aussi un évenement click. Donc qu'on soit
sur touch screen ou qu'on utilise une souris, cela va fonctionner */
function ajouteEcouteurSurDivCouleurs(listeDivsCouleurs) {
for(var i=0; i < listeDivsCouleurs.length; i++) {
var div = listeDivsCouleurs[i];
// Pour chaque div on ajoute un écouteur
div.addEventListener("click", function() {
// Pour récupérer la valeur d'une propriété CSS spécifiée dans
// une feuille de style séparée, il faut une petite fonction
var couleurChoisie = getStyle(this, "background-color");
context.strokeStyle = couleurChoisie;
console.log("Ecouteur sur div couleur selectionnée val =" + couleurChoisie);
});
}
}
// Piquée sur le web : http://www.quirksmode.org/dom/getstyles.html
function getStyle(el,styleProp)
{
if (el.currentStyle)
var y = el.currentStyle[styleProp];
else if (window.getComputedStyle)
var y = document.defaultView.getComputedStyle(el,null).getPropertyValue(styleProp);
return y;
}
// Appelée par l input field type=color, si on passe this comme parametre
// dans le "oninput=changeCouleur(this)", pas la peine de faire un
// getElementById ou un querySelector...
function changeCouleur(inputField) {
var color = inputField.value;
context.strokeStyle = color;
console.log("on change la couleur pour " + color);
}
function changeEpaisseur() {
var epaisseurTrait = document.querySelector("#selecteurEpaisseur").value;
context.lineWidth = epaisseurTrait;
console.log("on change l'apaisseur du trait pour " + epaisseurTrait);
}
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. |