So I found this matrix canvas and I’m trying to implement the javascript code in my starting passage using the <<script>>
macro, but it doesn’t work. It has this red error message saying: <<SCRIPT>>: BAD EVALUATION: UNEXPECTED TOKEN VAR
. It works just fine in the story Javascript section, but when I put the code in a passage it doesn’t work at all.
This isn’t the first time I’ve encountered this <<script>>
error either. Almost every time I try to use it on any sugarcube story, I get the error.
Here’s the starting passage’s content:
<div id="container"><canvas id="matrix"></canvas></div>
<div class="title"><img height="60px" src="https://i.imgur.com/28xuG9j.png"></div>
[[Start New Game->Start]]
<<nobr>>
<<script>>
'use strict'
var matrix = document.getElementById('matrix');
var ctx = matrix.getContext('2d');
var config = {
amount: 70, // of columns going down (it will top at this number)
speed: 100, // time between updates in ms (lower = faster)
size: 20, // in px
minLength: 5,
maxLength: 11,
firstColor: '#fff',
color: 'red'
}
var datarray = [];
var width = ctx.canvas.width = window.innerWidth;
var height = ctx.canvas.height = window.innerHeight;
/*
Data colum object
=============
*/
function Data(x) {
this.x = x
this.y = 0
this.history = []
this.historySizeMax = Math.floor(Math.random() * config.maxLength + config.minLength)
}
Data.prototype = {
update() {
this.y += config.size
if (this.y >= height + this.historySizeMax * config.size) {
datarray.splice(datarray.indexOf(this), 1)
putData()
}
this.history.unshift(String.fromCharCode(60 + Math.floor(Math.random() * 62)))
if (this.history.length > this.historySizeMax) this.history.pop()
},
draw() {
if(Math.random() > 0.995) return
ctx.fillStyle = config.firstColor
ctx.fillText(this.history[0], this.x, this.y)
ctx.fillStyle = config.color
for (var i = 1, char; char = this.history[i]; i++) {
ctx.fillText(char, this.x, this.y - i * config.size)
}
}
}
var count = Math.floor(width / config.size)
function putData() {
var newX = Math.floor(Math.random() * count) * config.size
for (var i = 0, row; row = datarray[i]; i++) {
if(row.x === newX && row.y - row.historySizeMax * config.size + config.size < config.size) return
}
datarray.push(new Data(newX))
}
/*
Init & loop
=============
*/
ctx.font = config.size + 'px Monospace'
ctx.shadowOffsetX = 1
ctx.shadowOffsetY = 1
ctx.shadowBlur = 6
ctx.shadowColor = config.color
setInterval(function() {
ctx.clearRect(0, 0, width, height)
if (datarray.length < config.amount) putData()
for (var i = 0, column; column = datarray[i]; i++) {
column.update()
column.draw()
}
}, config.speed)
<</script>>
<</nobr>>