Spaces:
Runtime error
Runtime error
File size: 7,506 Bytes
30c32c8 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 |
const BlockType = require('../../extension-support/block-type');
const ArgumentType = require('../../extension-support/argument-type');
const Cast = require('../../util/cast');
const uid = require('../../util/uid');
const Textures = {
Snow: require('./snow.png'),
Light: require('./light.png'),
Present: require('./present.png'),
};
/**
* Class for Extension blocks
* @constructor
*/
class Extension {
constructor(runtime) {
/**
* The runtime instantiating this block package.
* @type {Runtime}
*/
this.runtime = runtime;
/**
* @type {HTMLDivElement}
*/
this.mainContainer = null;
/**
* @type {HTMLCanvasElement}
*/
this.mainCanvas = null;
/**
* canvas context
* @type {CanvasRenderingContext2D}
*/
this.ctx = null;
this.initialize();
this.snowParticles = {};
this.lights = {};
this.runtime.on('RUNTIME_STEP_START', () => {
const viewBox = this.mainContainer.getBoundingClientRect();
for (const particleId in this.snowParticles) {
const particle = this.snowParticles[particleId];
const element = particle.element;
element.style.left = `calc(${particle.origin}% + ${particle.x}px)`;
particle.x -= 3;
const y = Cast.toNumber(element.style.top.replace('px', '')) + particle.speed;
element.style.top = `${y}px`;
if (element.getBoundingClientRect().right < 0 || y > viewBox.height) {
element.remove();
delete this.snowParticles[particleId];
}
}
this.drawLightBackground();
});
this.runtime.on('PROJECT_STOP_ALL', () => {
this.ctx.clearRect(0, 0, this.mainCanvas.width, this.mainCanvas.height);
this.clearSnow();
this.removeLights();
});
}
initialize() {
const mainContainer = document.body.appendChild(document.createElement("div"));
mainContainer.style = 'position: absolute;'
+ 'left: 0; top: 0; width: 100%; height: 100%;'
+ 'pointer-events: none; overflow: hidden;'
+ 'z-index: 1000009;';
this.mainContainer = mainContainer;
const mainCanvas = mainContainer.appendChild(document.createElement("canvas"));
mainCanvas.style = 'position: absolute;'
+ 'left: 0; top: 0; width: 100%; height: 100%;'
+ 'pointer-events: none; background: none;'
+ 'border: 0; margin: 0; padding: 0;'
+ 'z-index: 999999;';
this.mainCanvas = mainCanvas;
mainCanvas.width = 1280;
mainCanvas.height = 720;
const canvasContext = mainCanvas.getContext('2d');
this.ctx = canvasContext;
}
/**
* @returns {object} metadata for this extension and its blocks.
*/
getInfo() {
return {
id: 'jgChristmas',
name: 'Christmas',
color1: '#ff0000',
color2: '#00ff00',
blockIconURI: require('./icon.png'),
blocks: [
{
opcode: 'snow',
text: 'snow',
blockType: BlockType.COMMAND
},
{
opcode: 'clearSnow',
text: 'clear snow',
blockType: BlockType.COMMAND
},
// {
// opcode: 'addPresent',
// text: 'add present',
// blockType: BlockType.COMMAND
// },
// {
// opcode: 'removePresents',
// text: 'remove all presents',
// blockType: BlockType.COMMAND
// },
{
opcode: 'addLight',
text: 'add light',
blockType: BlockType.COMMAND
},
{
opcode: 'removeLights',
text: 'remove all lights',
blockType: BlockType.COMMAND
},
]
};
}
snow() {
const snowImage = this.mainContainer.appendChild(document.createElement("img"));
const size = Math.round(8 + (Math.random() * 16));
const opacity = 0.5 + (Math.random() / 2);
const originX = Math.random() * 150;
snowImage.style = 'position: absolute;'
+ `left: ${originX}%; top: -${size}px; width: ${size}px; height: ${size}px;`
+ `pointer-events: none; opacity: ${opacity};`
+ 'z-index: 1000008;';
snowImage.src = Textures.Snow;
const id = uid();
this.snowParticles[id] = {
element: snowImage,
origin: originX,
x: 0,
size: size,
speed: 2 + Math.random() * 6
};
}
removeLights() {
this.ctx.clearRect(0, 0, this.mainCanvas.width, this.mainCanvas.height);
for (const particleId in this.lights) {
const particle = this.lights[particleId];
const element = particle.element;
element.remove();
delete this.lights[particleId];
}
}
addLight() {
const lightImage = this.mainContainer.appendChild(document.createElement("img"));
const viewBox = this.mainContainer.getBoundingClientRect();
const originX = Math.random() * viewBox.width;
const originY = Math.random() * viewBox.height;
const direction = Math.random() * 360;
lightImage.style = 'position: absolute;'
+ `left: 0; top: 0; width: 70px; height: 70px;`
+ `transform: translate(${originX}px, ${originY}px) rotate(${direction}deg);`
+ `transform-origin: 34px 41px; pointer-events: none;`
+ 'z-index: 1000005;';
lightImage.src = Textures.Light;
const id = uid();
this.lights[id] = {
element: lightImage,
x: originX,
y: originY
};
this.drawLightBackground();
let filterGreen = false;
setInterval(() => {
lightImage.style.filter = filterGreen ? 'hue-rotate(90deg) brightness(1.5)' : '';
filterGreen = !filterGreen;
}, 700);
}
drawLightBackground() {
const canvas = this.mainCanvas;
const viewBox = canvas.getBoundingClientRect();
const ctx = this.ctx;
ctx.clearRect(0, 0, canvas.width, canvas.height);
ctx.strokeStyle = '#3D5C3A';
ctx.lineWidth = 1;
ctx.moveTo(0, 70);
ctx.beginPath();
for (const particleId in this.lights) {
const particle = this.lights[particleId];
ctx.lineTo(((particle.x + 34) / viewBox.width) * 1280, ((particle.y + 41) / viewBox.height) * 720);
ctx.moveTo(((particle.x + 34) / viewBox.width) * 1280, ((particle.y + 41) / viewBox.height) * 720);
ctx.stroke();
}
}
clearSnow() {
for (const particleId in this.snowParticles) {
const particle = this.snowParticles[particleId];
const element = particle.element;
element.remove();
delete this.snowParticles[particleId];
}
}
addPresent() {
}
}
module.exports = Extension;
|