Future Programmer

Graphics - Clear Screen

February 2, 2013

This tutorial shows you how to clear the screen. The other day I ran into an issue of having to clear a 640x400 screen and writing every pixel is just far too much data to do it timely. So I came up with just replaceing the image data with a fresh image data. The following tutorials will work towards making a graphics library so there is a little more code to this than there normally would be.

cls.js Left Click and select save file as to download var Graphics = { "Data":0, "Width":0, "Height":0, "WidthInBytes":0, "BytesPerPixel":4, "Context":0, "PutPixel": function(X, Y, Color) { var Index = (Y * this.Data.width + X) * this.BytesPerPixel; this.Data.data[Index+0] = Color[0]; this.Data.data[Index+1] = Color[1]; this.Data.data[Index+2] = Color[2]; this.Data.data[Index+3] = 0xFF; }, "_Octant0": function(X1, Y1, DeltaX, DeltaY, XDirection, Color) { DeltaYx2 = DeltaY * 2; DeltaYx2MinusDeltaXx2 = DeltaYx2 - (DeltaX * 2); ErrorTerm = DeltaYx2 - DeltaX; this.PutPixel(X1, Y1, Color); while(DeltaX--) { if (ErrorTerm >= 0) { Y1++; ErrorTerm += DeltaYx2MinusDeltaXx2; } else { ErrorTerm += DeltaYx2; } X1 += XDirection; this.PutPixel(X1, Y1, Color); } }, "_Octant1": function(X1, Y1, DeltaX, DeltaY, XDirection, Color) { //alert('Octant1 '+DeltaY); DeltaXx2 = DeltaX * 2; //0 DeltaXx2MinusDeltaYx2 = DeltaXx2 - (DeltaY * 2); // 0 ErrorTerm = DeltaXx2 - DeltaY; // -5 this.PutPixel(X1, Y1, Color); while(DeltaY--) { if (ErrorTerm >= 0) { X1 += XDirection; ErrorTerm += DeltaXx2MinusDeltaYx2; } else { ErrorTerm += DeltaXx2; // -5 } Y1++; this.PutPixel(X1, Y1, Color); } }, "Line": function(X1, Y1, X2, Y2, Color) { if (Y1 > Y2) { Temp = Y1; Y1 = Y2; Y2 = Temp; Temp = X1; X1 = X2; X2 = Temp; } DeltaX = X2 - X1; DeltaY = Y2 - Y1; if (DeltaX > 0) { if (DeltaX > DeltaY) { this._Octant0(X1, Y1, DeltaX, DeltaY, 1, Color); } else { this._Octant1(X1, Y1, DeltaX, DeltaY, 1, Color); } } else { DeltaX = -DeltaX; if (DeltaX > DeltaY) { this._Octant0(X1, Y1, DeltaX, DeltaY, -1, Color); } else { this._Octant1(X1, Y1, DeltaX, DeltaY, -1, Color); } } }, "Cls": function() { this.Data = this.Context.createImageData(this.Width, this.Height); return this.Data; }, "New": function(Context, Width, Height) { this.Context = Context; this.Width = Width; this.WidthInBytes = Width * this.BytesPerPixel; this.Height = Height; this.Data = Context.createImageData(Width, Height); return this.Data; }, "Update": function() { this.Context.putImageData(this.Data, 0, 0); } } window.onload = function() { var Width = 640; var Height = 400; var canvas = document.createElement("canvas"); var context = canvas.getContext("2d"); canvas.setAttribute("width", Width); canvas.setAttribute("height", Height); canvas.style.border = "solid 1px #CCC"; canvas.style.margin = "auto"; canvas.style.background = "#000"; document.body.appendChild(canvas); document.body.style.textAlign = 'center'; Graphics.New(context, Width, Height); Graphics.PutPixel(320, 200, [0x66, 0x99, 0xCC]); Graphics.PutPixel(322, 201, [0x66, 0x99, 0xCC]); Graphics.PutPixel(321, 202, [0x66, 0x99, 0xCC]); Graphics.PutPixel(318, 199, [0x66, 0x99, 0xCC]); Graphics.PutPixel(319, 198, [0x66, 0x99, 0xCC]); Graphics.Cls(); Graphics.Line(0, 0, 320, 400, [0x66, 0x99, 0xCC]); Graphics.Line(640, 0, 320, 400, [0x66, 0x99, 0xCC]); Graphics.Update(); } cls.htm Left Click and select save file as to download <!DOCTYPE html> <html> <head> <title>Future Programmer</title> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <meta http-equiv="X-UA-Compatible" content="IE=9" /> <script src="cls.js" type="text/javascript"></script> </head> <body> </body> </html>