Skip to content

Commit

Permalink
added in-canvas text hints and marquee
Browse files Browse the repository at this point in the history
  • Loading branch information
Elixonus committed Jul 26, 2024
1 parent 4566968 commit b19b936
Showing 1 changed file with 92 additions and 3 deletions.
95 changes: 92 additions & 3 deletions optics/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -1339,6 +1339,8 @@ const misclickSound = document.getElementById("sound-misclick");
const switchSound = document.getElementById("sound-switch");
let fullscreen = false;
let glow = true;
let hint = true;
let textAnimation = new NumberAnimation([new AnimationKeyframe(0, 0), new AnimationKeyframe(25, 0), new AnimationKeyframe(125, 1), new AnimationKeyframe(150, 1), new AnimationKeyframe(250, 0)], 250, 0, interpolateLinear, true);
const pointOrigin = new Point(0, 0);
const cameraPosition = pointOrigin.clone();
const targetCameraPosition = cameraPosition.clone();
Expand Down Expand Up @@ -1645,7 +1647,7 @@ function render() {
ctx.restore();
}

// draw text overlay if user is currently dragging an object
// draw user action text overlay if user is currently dragging an object
if (scene.draggedObject !== false) {
let text = undefined;
let doDrawText = true;
Expand Down Expand Up @@ -1724,6 +1726,7 @@ function render() {
}

// find the width of the text in pixels to be used for centering the text
ctx.font = "50px Verdana";
let textWidth = ctx.measureText(text).width;

// render the text overlay's background
Expand All @@ -1733,15 +1736,101 @@ function render() {

// render the text overlay's text
ctx.fillStyle = "#ffffff";
ctx.font = "50px Verdana";
ctx.fillText(text, scene.draggedObject.position.x - textWidth / 2, scene.draggedObject.position.y + 25 + extraSpace);
}
}

// go from world space to canvas space
ctx.resetTransform();

ctx.shadowBlur = 0;

// draw hint text overlay if hint is set to true
if (hint) {
let text = undefined;

if (mouseAction === MouseAction.drag) {
if (scene.draggedObject === false) {
text = "Click and drag an object to change the position.";
} else {
if (!scene.draggedObject.hasOwnProperty("positionAnimation") || scene.draggedObject.positionAnimation === undefined) {
text = "Drag the object to change the position.";
} else {
text = "The object position animation is currently in progress.";
}
}
} else if (mouseAction === MouseAction.dragX) {
if (scene.draggedObject === false) {
text = "Click and drag an object horizontally to change the position.";
} else {
if (!scene.draggedObject.hasOwnProperty("positionAnimation") || scene.draggedObject.positionAnimation === undefined) {
text = "Drag the object horizontally to change the position.";
} else {
text = "The object's position animation is currently in progress.";
}
}
} else if (mouseAction === MouseAction.dragY) {
if (scene.draggedObject === false) {
text = "Click and drag an object vertically to change the position.";
} else {
if (!scene.draggedObject.hasOwnProperty("positionAnimation") || scene.draggedObject.positionAnimation === undefined) {
text = "Drag the object vertically to change the position.";
} else {
text = "The object's position animatoin is currently in progress.";
}
}
} else if (mouseAction === MouseAction.rotate) {
if (scene.draggedObject === false) {
text = "Click and mouse-drag an object to change the rotation.";
} else {
if (!scene.draggedObject.hasOwnProperty("rotationAnimation") || scene.draggedObject.rotationAnimation === undefined) {
text = "Drag the mouse to change the rotation.";
} else {
text = "The object's rotation animation is currently in progress.";
}
}
} else if (mouseAction === MouseAction.change) {
if (scene.draggedObject === false) {
text = "Click and mouse-drag an object to change the property.";
} else if (scene.draggedLaser !== false) {
text = "Drag the mouse vertically to switch the power.";
} else if (scene.draggedMirror !== false) {
text = "Drag the mouse vertically to change the type of behavior of light.";
} else if (scene.draggedGuide !== false) {
text = "Drag the mouse vertically to switch the tool.";
}
} else if (mouseAction === MouseAction.laser) {
text = "Click and hold to create a laser of a certain rotation.";
} else if (mouseAction === MouseAction.interferer) {
text = "Click and hold to create an interferer of a certain type.";
} else if (mouseAction === MouseAction.guide) {
text = "Click and hold to create a ruler or protractor of a certain rotation.";
}

ctx.font = "50px Verdana";
let textWidth = ctx.measureText(text).width;

ctx.save();
ctx.beginPath();
ctx.rect(960 - 600 - 5, 50 - 25 - 25, 1200 + 10, 50 + 50);
ctx.clip();

if (textWidth >= 1200) {
let textNumber = textAnimation.getValues();
ctx.translate((textWidth / 2 - 600) * (1 - 2 * textNumber), 0);
textAnimation.animate();
}

// render the text overlay's background
ctx.shadowBlur = 0;
ctx.fillStyle = "#000000";
ctx.fillRect(960 - textWidth / 2 - 5, 50 - 25 - 5, textWidth + 10, 50 + 10);

// render the text overlay's text
ctx.fillStyle = "#ffffff";
ctx.fillText(text, 960 - textWidth / 2, 50 + 25);
ctx.restore();
}

// go from canvas space to button space
ctx.translate(0, 540);

Expand Down

0 comments on commit b19b936

Please sign in to comment.