adapt the following js to react lifecycle and use it to set the background of my…
- User
adapt the following js to react lifecycle and use it to set the background of my Header.js. JS code: (function () { var requestAnimationFrame = window.requestAnimationFrame || window.mozRequestAnimationFrame || window.webkitRequestAnimationFrame || window.msRequestAnimationFrame || function (callback) { window.setTimeout(callback, 1000 / 60); }; window.requestAnimationFrame = requestAnimationFrame; })();
// Terrain stuff. var background = document.getElementById(“bgCanvas”), bgCtx = background.getContext(“2d”), width = window.innerWidth, height = document.body.offsetHeight;
(height < 400) ? height = 400 : height;
background.width = width; background.height = height;
function Terrain(options) { options = options || {}; this.terrain = document.createElement(“canvas”); this.terCtx = this.terrain.getContext(“2d”); this.scrollDelay = options.scrollDelay || 90; this.lastScroll = new Date().getTime();
this.terrain.width = width; this.terrain.height = height; this.fillStyle = options.fillStyle || “#191D4C”; this.mHeight = options.mHeight || height;
// generate this.points = [];
var displacement = options.displacement || 140, power = Math.pow(2, Math.ceil(Math.log(width) / (Math.log(2))));
// set the start height and end height for the terrain this.points[0] = this.mHeight;//(this.mHeight - (Math.random() * this.mHeight / 2)) - displacement; this.points[power] = this.points[0];
// create the rest of the points for (var i = 1; i < power; i *= 2) { for (var j = (power / i) / 2; j < power; j += power / i) { this.points[j] = ((this.points[j - (power / i) / 2] + this.points[j + (power / i) / 2]) / 2) + Math.floor(Math.random() * -displacement + displacement); } displacement *= 0.6; }
document.body.appendChild(this.terrain); }
Terrain.prototype.update = function () { // draw the terrain this.terCtx.clearRect(0, 0, width, height); this.terCtx.fillStyle = this.fillStyle;
if (new Date().getTime() > this.lastScroll + this.scrollDelay) { this.lastScroll = new Date().getTime(); this.points.push(this.points.shift()); }
this.terCtx.beginPath(); for (var i = 0; i <= width; i++) { if (i === 0) { this.terCtx.moveTo(0, this.points[0]); } else if (this.points[i] !== undefined) { this.terCtx.lineTo(i, this.points[i]); } }
this.terCtx.lineTo(width, this.terrain.height); this.terCtx.lineTo(0, this.terrain.height); this.terCtx.lineTo(0, this.points[0]); this.terCtx.fill(); }
// Second canvas used for the stars bgCtx.fillStyle = ‘#05004c’; bgCtx.fillRect(0, 0, width, height);
// stars function Star(options) { this.size = Math.random() * 2; this.speed = Math.random() * .05; this.x = options.x; this.y = options.y; }
Star.prototype.reset = function () { this.size = Math.random() * 2; this.speed = Math.random() * .05; this.x = width; this.y = Math.random() * height; }
Star.prototype.update = function () { this.x -= this.speed; if (this.x < 0) { this.reset(); } else { bgCtx.fillRect(this.x, this.y, this.size, this.size); } }
- Assistant
- User
complete the code by showing me snippets of how to: // Definitions for Star, ShootingStar, and Terrain classes not included for brevity but should be adapted with constructors that accept dimensions and context (as illustrated in this example).
- Assistant
- User
at which point in the new Header.js should i added the code you just generated
- Assistant
In the refactored React
Headercomponent I described previously, the completion snippets, which define theStar,ShootingStar, and classes, should be included within the same file as the component but outside of it. Ideally, you would place these class definitions before the definition of the component itself. - User
- Assistant