$\require{cancel}$
context.fillStyle = value is an example.
<canvas id = "demo1"
width= "200"
height= "60"
style="border:1px solid black;">
</canvas>
<script>
"use strict"
const canvas = document.getElementById("demo1")
const ctx = canvas.getContext("2d")
ctx.fillRect(10,10,20,20)
ctx.fillStyle = "red"
ctx.fillRect(40,40,10,10)
ctx.fillStyle = "#00ff00"
ctx.fillRect(60,40,10,10)
ctx.fillStyle = "rgb(0,0,255)"
ctx.fillRect(80,40,10,10)
ctx.fillStyle = "hsl(20,100%,50%)"
ctx.fillRect(100,40,10,10)
let color = "rgb("
+ (Math.random()*255).toString() + ","
+ (Math.random()*255).toString() + ","
+ (Math.random()*255).toString() + ")"
ctx.fillStyle = color
ctx.fillRect(120,40,10,10)
</script>
context.clearRect(0,0,width, height)
context.clearRect(0,0,canvas.width, canvas.height)
x = Math.random() returns a value $ 0 \le x \lt 1$.
Math.PI
function RandomInt(max) {
return Math.floor(Math.random() * max)
}
let starArray = [] creates an empty array.
const colors = ["red", "green", "blue", "cyan", "magenta", "yellow"] creates an initialized constant array.
colors.length
function RandomColor() {
const size = colors.length
return colors[RandomInt(size)]
}
.push appends the element to the end of the array.
<script>
let starArray = []
const colors = ["red", "green", "blue", "cyan", "magenta", "yellow"]
console.log("star array:", starArray, "is size ", starArray.length)
console.log("colors:", colors, "is size ", colors.length)
starArray.push("Mickey Mouse")
starArray.push(Math.PI)
starArray.push(colors)
console.log("star array:", starArray, "is size ", starArray.length)
console.log("Chainging colors")
colors.push("CornflowerBlue")
console.log("star array:", starArray, "is size ", starArray.length)
</script>
function LogIt(item, pos) {
console.log("at ", pos, " is the value : ", item)
}
colors.forEach(LogIt)
let star = {x: 10, y: 20}
console.log(star, "star.x= ", star.x, 'star["y"] = ', star["y"])
star.age = 4
star["size"] = "small"
Object.keys(star) provides an array of the keys
Object.entries(star) returns the array [key, value]
console.log("Object.keys.forEach: \n")
Object.keys(star).forEach(LogIt)
console.log("Object.entries: \n")
for( let [key, value] of Object.entries(star)) {
console.log(key, value)
}
<script>
console.log("\n\nObjects")
let star = {x: 10, y:20}
console.log(star, "star.x= ", star.x, 'star["y"] = ', star["y"])
star.age = 4
star['size'] = "small"
console.log(star)
console.log("\nObject.keys: ")
console.log(Object.keys(star))
console.log("\nObject.keys.forEach: ")
Object.keys(star).forEach(LogIt)
console.log("\nObject.entries: ")
for( let [key, value] of Object.entries(star)) {
console.log(key, value)
}
</script>
growing and change is a method to keep track of if we are going up or down in size.