Showing posts with label javascript. Show all posts
Showing posts with label javascript. Show all posts

Saturday, June 13, 2020

JavaScript blessings and curses

Can you spot the difference between the following two JavaScript lines:

ctx.fillstyle = "green";
ctx.fillStyle = "green";

The first line does not give you an error even tough ctx does not have a fillstyle field (note the small "s"), it just adds another field to ctx! Being not strongly typed is the blessing and curse of JS (more a curse when the code gets larger). When you use the first line you will get a black circle because black is the default color.


Monday, April 27, 2020

JavaScript: Adding to arrays with "..."

In JavaScript, push method is used to add to an array. Let's say you have an existing array called out  = [{e1: 5}, {e2:10}] and want to add its elements to an array called arr. Results differ for arr.push(out) and arr.push(...out). When you use ellipsis (...), each element of out array will be added as a separate element to arr and arr.length will increase by 2 after push. Without ..., out will be added as a single element to arr, and arr.length will increase by 1 after push operation. Below is an example script:


Friday, July 26, 2019

Asteroids game with JavaScript

Recently I watched the Make JavaScript Asteroids in less than one hour video and decided to give it a go. I changed the ship shape to make it clearer which side is the nose, added thrust animations and sound effects. Most of the sounds are my son's recordings. I did the thrust sounds with Audacity's pink and brown noises. My code is on GitHub. Game play in web browser video:

Friday, May 31, 2019

NodeJS HTTP Server Timeout

In Nodejs an HTTP server created by http.createServer() has a default timout of 2 minutes (12000ms). I have an HTML client that calls the server through a GET request and the server starts a batch file that takes more than 5 minutes to finish. After two minutes, the server hits the timout and does some sort of restart (I guess), calls the batch file again and this continues ad infinitum, none of the client GET requests getting a return!

Solution: server.setTimeout(0); //disables timeout

Tuesday, October 09, 2018

Simple login demo with NodeJS

I wrote a simple / bare bones login demo with NodeJS. It uses cookies to remember the user. Note that this is the absolute minimum and should not be used in applications requiring security.

Note that in login.html we have to use HTTP POST (not GET. This line in login.html: form action="/login" method="post) so that user name and pass is not displayed in browser address bar!

Thursday, November 24, 2016

Converting a desktop game to a web app

Two years ago, I wrote a simple desktop game, Save My Cheese, mainly to explore the A* pathfinding algorithm. In the game you are trying to block the path of mice by puzzle pieces and prevent the mice from reaching the cheese. When you finish the puzzle before the mice reach the cheese, you finish that level.