HTML preprocessors can make writing HTML more powerful or convenient. For instance, Markdown is designed to be easier to write and read for text documents and you could write a loop in Pug.
In CodePen, whatever you write in the HTML editor is what goes within the <body>
tags in a basic HTML5 template. So you don't have access to higher-up elements like the <html>
tag. If you want to add classes there that can affect the whole document, this is the place to do it.
In CodePen, whatever you write in the HTML editor is what goes within the <body>
tags in a basic HTML5 template. If you need things in the <head>
of the document, put that code here.
The resource you are linking to is using the 'http' protocol, which may not work when the browser is using https.
CSS preprocessors help make authoring CSS easier. All of them offer things like variables and mixins to provide convenient abstractions.
It's a common practice to apply CSS to a page that styles elements such that they are consistent across all browsers. We offer two of the most popular choices: normalize.css and a reset. Or, choose Neither and nothing will be applied.
To get the best cross-browser support, it is a common practice to apply vendor prefixes to CSS properties and values that require them to work. For instance -webkit-
or -moz-
.
We offer two popular choices: Autoprefixer (which processes your CSS server-side) and -prefix-free (which applies prefixes via a script, client-side).
Any URLs added here will be added as <link>
s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.
You can apply CSS to your Pen from any stylesheet on the web. Just put a URL to it here and we'll apply it, in the order you have them, before the CSS in the Pen itself.
You can also link to another Pen here (use the .css
URL Extension) and we'll pull the CSS from that Pen and include it. If it's using a matching preprocessor, use the appropriate URL Extension and we'll combine the code before preprocessing, so you can use the linked Pen as a true dependency.
JavaScript preprocessors can help make authoring JavaScript easier and more convenient.
Babel includes JSX processing.
Any URL's added here will be added as <script>
s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.
You can apply a script from anywhere on the web to your Pen. Just put a URL to it here and we'll add it, in the order you have them, before the JavaScript in the Pen itself.
If the script you link to has the file extension of a preprocessor, we'll attempt to process it before applying.
You can also link to another Pen here, and we'll pull the JavaScript from that Pen and include it. If it's using a matching preprocessor, we'll combine the code before preprocessing, so you can use the linked Pen as a true dependency.
Search for and use JavaScript packages from npm here. By selecting a package, an import
statement will be added to the top of the JavaScript editor for this package.
Using packages here is powered by esm.sh, which makes packages from npm not only available on a CDN, but prepares them for native JavaScript ESM usage.
All packages are different, so refer to their docs for how they work.
If you're using React / ReactDOM, make sure to turn on Babel for the JSX processing.
If active, Pens will autosave every 30 seconds after being saved once.
If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.
If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.
Visit your global Editor Settings.
<!DOCTYPE html>
<html>
<head>
<meta charset=utf-8 />
<title>JS Bin</title>
</head>
<body>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>animazione mostro</title>
<style>
canvas{
border:0px solid;
}
body{
background:#CFF;
}
</style>
</head>
<body onload="init();">
<canvas width="1200" height="500" id="mycanvas"></canvas>
<script>
// Michel : I added a global var rectColor here
var x, y, speed, canvas, context, mousePos, rectColor='red';
reqAnimFrame = window.mozRequestAnimationFrame ||
window.webkitRequestAnimationFrame ||
window.msRequestAnimationFrame ||
window.oRequestAnimationFrame
;
function init() {
x = 200;
y = 150;
speed = 4;
canvas = document.getElementById("mycanvas");
context = canvas.getContext("2d");
// Michel : add these listeners here, the canvas will be defined for sure
// as the init method is called only when the page is loaded
canvas.addEventListener('mousedown', paint_redrect_yellow);
canvas.addEventListener('mouseup', paint_redrect_red);
canvas.addEventListener('mousemove', function (evt) {
mousePos = getMousePos(canvas, evt);
}, false);
animate();
}
// MICHEL : you just need to set the global variable, not set the style or do
// any drawing. The global variable will be takjen into account by the
// drawMonster function
function paint_redrect_yellow() {
rectColor='yellow';
}
function paint_redrect_red() {
rectColor='red';
}
function animate() {
context.clearRect(0, 0, canvas.width, canvas.height);
drawMonster();
x += speed;
if(x <= 110 || x >= 655){
speed = -speed;
}
reqAnimFrame(animate);
}
function drawMonster() {
context.save();
context.translate(x, y);
context.beginPath()
context.fillStyle ='rgba(51,51,255,1)';
context.fillRect(0, 0, 400, 250);
<!--red rectangle -->
// Michel : use the global variable here
context.fillStyle =rectColor;
context.fillRect(25,25,350,200);
//right yellow eye
context.beginPath();
context.arc(300,0,70,0, Math.PI/0.5);
context.fillStyle="yellow";
context.fill()
context.strokeStyle = "#3333ff";
context.lineWidth = 5;
context.stroke();
//right black eye
/*context.beginPath();
context.arc(280,0,30,0, Math.PI/0.5);
context.fillStyle="#000000";
context.fill()
*/
//left yellow eye
context.beginPath();
context.arc(100,0,70,0, Math.PI/0.5);
context.fillStyle="yellow";
context.fill()
context.strokeStyle = "#3333ff";
context.lineWidth = 5;
context.stroke();
<!--sopracciglia sn -->
context.beginPath();
context.arc(115,-15,80,0,5, true);
context.strokeStyle="#000000";
context.lineWidth = 13;
context.lineCap ="round";
context.stroke();
<!--sopracciglia dx -->
context.beginPath();
context.arc(290,-20,80,4.2,9.3, true);
context.strokeStyle="#000000";
context.lineWidth = 13;
context.stroke();
<!--mouse first part -->
context.beginPath();
context.moveTo(50,120);
context.quadraticCurveTo(50,150,200,200);
<!--mouse second part -->
context.quadraticCurveTo(350,240,350,150);
context.closePath();
context.save();
context.fillStyle='black';
context.fill();
context.restore();
<!--teeth above-->
context.beginPath()
context.moveTo(180,135);
context.lineTo(190,180);
context.lineTo(210,138);
context.moveTo(245,142);
context.lineTo(290,180);
context.lineTo(330,150);
<!--teeth below -->
context.moveTo(260,210);
context.lineTo(210,200);
context.lineTo(240,180);
context.fillStyle ="#fff";
context.fill();
<!--left arm -->
context.beginPath()
context.moveTo(-50,150);
context.lineTo(0,150);
<!--right arm -->
context.moveTo(480,180);
context.lineTo(400,180);
context.lineWidth = 15;
context.strokeStyle = "#3333ff";
context.lineCap ="round";
context.stroke();
<!--tenaglia braccio sn -->
context.beginPath()
context.arc(-80,150,30,0, Math.PI/1.5, true);
<!--tenaglia braccio dx -->
context.moveTo(480,150);
context.lineTo(480,230);
context.lineTo(540,230);
context.lineTo(540,180);
context.lineWidth = 15;
context.stroketyle="#000000";
context.stroke()
context.restore();
// You told me I have to change x, y + the drawings in the drawEyeThatFollowsMousePos
// so that the eye has the right colors/position
// I was only able to change the radius but if i put number instead of x and y the eye does not follow the animation anymore
//What's wrong with this?
// Michel ; try x+ something, like the x+150 below
if(mousePos != undefined) {
drawEyeThatFollowsMousePos(x+100, y, 50, mousePos.x, mousePos.y)
drawEyeThatFollowsMousePos(x+300, y, 50, mousePos.x, mousePos.y)
} else {
// If the mouse is not here, look at 0, 0 for example...
drawEyeThatFollowsMousePos(x+100, y, 50, 300, 300)
drawEyeThatFollowsMousePos(x+300, y, 50, 300, 300)
}
}
function getMousePos(canvas, evt) {
var obj = canvas;
var top = 0;
var left = 0;
while (obj && obj.tagName != 'BODY') {
top += obj.offsetTop;
left += obj.offsetLeft;
obj = obj.offsetParent;
}
var mouseX = evt.clientX - left + window.pageXOffset;
var mouseY = evt.clientY - top + window.pageYOffset;
return {
x:mouseX,
y:mouseY
};
}
function drawEyeThatFollowsMousePos(cx, cy, radius, mouseX, mouseY) {
// Draw external eye outline
context.beginPath();
context.arc(cx, cy, 50, 0, 2*Math.PI, false);
context.stroke();
var angle = Math.atan2(cx-mouseX, cy-mouseY)
var dist = distanceFromEyeCenterForDrawingEye(cx, cy, mouseX, mouseY, 15);
var dx = dist*Math.sin(angle);
var dy = dist*Math.cos(angle);
context.beginPath();
context.arc(cx-dx, cy-dy, 30, 0, 2*Math.PI, false);
context.fill();
}
function distanceFromEyeCenterForDrawingEye(cx, cy, x, y, treshold) {
var xd=(cx-x), yd=(cy-y);
var distMouseEyeCenter = Math.sqrt(xd*xd + yd*yd);
return Math.min(treshold, distMouseEyeCenter);
}
</script>
</body onload="init();">
</html>
</body>
</html>
Also see: Tab Triggers