JavaScript is used to add graphics or interactive pieces to your website, compared to HTML, which is used to create the foundations of a website. For example, JavaScript can edit pieces of text (known as “strings” in programming). For example, we take the string “Player 1: “ and join it to the name variable to create the complete text label, e.g. “Player 1: Chris”. JavaScript can also be used to run code in response to certain events occurring on a web page.
```javascript function setup() { createCanvas(400, 400); }
function draw() { background(220); ellipse(50,50,80,80); } ‘’’
var s = "JavaScript syntax highlighting";
alert(s);
function setup() {
createCanvas(1500, 1500);
}
function draw() {
let inside = color(2, 200, 89)
let inside2 = color(2, 400, 89)
let p = createP('hey there');
p.style('font-size', '30px');
p.style('width', '65px');
p.style('text-align', 'CENTRE');
p.position(mouseX, mouseY, 0);
if (mouseIsPressed) {
fill(inside);
}else{
fill(inside2);
strokeWeight(0);
}
createP(mouseX, mouseY, 100)
ellipse(mouseX, mouseY, 180);
}
var s = "JavaScript syntax highlighting";
alert(s);
// draw a spinning box
// with width, height and depth of 50
// this variable will hold our webcam video
let cam;
function setup() {
createCanvas(1500, 1500, WEBGL);
cam = createCapture(VIDEO);
cam.size(710, 400);
describe('a box rotating in 3D space with specular highlight. Clicking the mouse toggles the specular highlight color between rgb(255,255,255) and the default rgb(255,255,255).');
}
function draw() {
background(0);
// move your mouse to change light position
let locX = mouseX - width / 2;
let locY = mouseY - height / 2;
// to set the light position,
// think of the world's coordinate as:
// -width/2,-height/2 ----------- width/2,-height/2
// | |
// | 0,0 |
// | |
// -width/2,height/2 ----------- width/2,height/2
pointLight(250, 250, 250, locX, locY, 50);
texture(cam);
rotateX(0.5);
noStroke();
sphere(200);
background(200);
rotateX(frameCount * 0.008);
rotateY(frameCount * 0.008);
box(mouseX, mouseY, 100);
}