I recently developed my first web game: an HTML5 video puzzle. The development process was interesting. I like programming, but after implementing the game logic, I had an interesting idea: Why not find a way to hide the code? At first I thought of something very simple, such as disabling the context menu so that the source code of the page could be viewed when right-clicking. But this makes no sense, the right-click menu does not work, and people can still view the source code through keyboard shortcuts or "View Source" in the menu bar.
A picture can say a thousand words
It depends on the size of the picture. But I decided to encrypt the source code and store it in an image. The HTML5 canvas component is very suitable for this kind of thing because it supports operations on image pixels. A pixel is represented by four values (channels): red, green, blue and alpha channel. Their values are distributed from 0 to 255. My Javascript code is a character, and each character has an ASCII corresponding value. The range of ASCII values is also 0-255, so what I want to do is to traverse each pixel on the canvas and set the ASCII value of 3 code characters for each pixel as its RGB value. You can easily do this through the charCodeAt function. Take out these characters.