A framework for JavaScript canvas
##IMPORTANT NOTICE
Since the version 2.0.6 the log
function, that prints messages into the developer console, has been renamed to print
, and the logh
function, which returns the logarithm of the given number, has been renamed to log
. This can create errors when upgrading from version 2.0.5 and below to version 2.0.6 and up. Be wary of that
Also, the versions below the version 2.0.6 are no longer available through the CDN due to a critical error. The lowest version you can now use is 2.0.6
- mobile - variable that is
true
if the code is opened from a mobile device orfalse
if it is not; useful if you have special control buttons for mobile devices - width and height - contain the width and height of the canvas element
- innerWidth and innerHeight - contain the inner width and inner height of the browser window
- mouseX and mouseY - contain the current coordinates of the mouse on the screen
- PI - Pi constant
- TWO_PI - two times Pi
- HALF_PI - half Pi
- E - Euler's constant
- SQRT2 - square root of 2
- SQRT1_2 - square root of 1/2
- LN2 - natural logarithm of 2
- LN10 - natural logarithm of 10
- LOG2E - base 2 logarithm of e
- LOG10E - base 10 logarithm of e
- frameCount - contains the number of frames that have passed since the loop started running
- frameRate - contains the current number of frames per second
- mousePressed - equals to
true
if the mouse is currently being pressed orfalse
if it isn't - Storage.save(name, data) - saves data under the given name in the local storage
- Storage.load(name) - return the data saved in the local storage under the given name
- Storage.removeItem(name) - deletes the saved data under the given name
- Storage.clear() - clears all the data saved in the local storage
- Storage.itemAtIndex(index) - return the data saved under the given index. Comes in handy when you need to loop through saved data
- autoUpdate(object) - automatically updates the object that is passed to it; usually called whilst creating an isntance of a class by calling
autoUpdate(this)
; requires the object to have theupdate
method - stopAutoUpdate(object) - removes the given object from the auto update list
setup()
- main function when the documents has been fully loadedupdate()
- main function called 60 times per second by default; can be changed using the framerate functionpreload()
- runs before anything else; useful for preloading imagesinclude(path, callback)
- includes another JavaScript file for further use; callback function is optional and is called after the file has been loadednoCanvas()
- creates a project without a canvas that still uses the update and setup functionsframerate(newFramerate)
- changes the framerate to the new onesetTitle(title)
- sets the title of the pagemap(num, a, b, c, d)
- takes a number that is in range from a to b numbers and maps it to the range from c to drandom(num1, num2)
- returns a random number in the range of num1 to num2; if num2 is null, it returns a random number from 0 to num1, while never returning the maximum value; if both num1 and num2 are null, it returns a random number from 0 to 1; if an array is passed to the random function, it will return a random element from that arrayrandInt(num1, num2)
- same as random(), except it returns a whole number; you must provide at least one argumentrandomizeColor(r, g, b)
- returns a random shade of the given color as an array of [red, green, blue] valuesfloor(num)
- rounds the given number to the next lower integerceil(num)
- rounds the given number to the next higher integerround(num)
- rounds the given number mathematicallypow(num, pow)
- returns num to the power of powsqrt(num)
- returns the square root of the given numbersqr(num)
- returns the square of given numberabs(num)
- returns the absolute value of the given numbersin(angle)
- returns the sine of given anglecos(angle)
- returns the cosine of given angleasin(num)
- returns the arcus sine of the given numberacos(num)
- returns the arcus cosine of the given numbertan(angle)
- returns the tangent of the given angleatan(num)
- returns the arcus tangnet of the given numberexp(num)
- return E to the power of numlog(num)
- returns the natural logarith of num with base Emin(nums)
- returns the lowest of the numbers given; takes multiple numbers as inputmax(nums)
- returns the highest of the numbers given; takes multiple numbers as inputwrite(text)
- writes the given text to the documentprint(text)
- logs the given text to the consoletable(array)
- displays the array in a table in the consoleerror(text)
- writes the given text to the console as an errorwarn(text)
- writes the given text to the console as a warningsetText(element, text)
- writes the given text to the element with the given IDkeyDown()
- activates when a key is pressed; has a built in variable called keyCode which contains the code of the pressed keykeyUp()
- activates when a key is released; has a built in variable called keyCode which contains the code of the released keytouchStart()
- activates only on touchscreen devices when the screen has been touched; it is given an array as an argument which contains all the information about all the touches of the screentouchMove()
- activates only on touchscreen devices when the screen is touched and a touch is moving on the screen; it is given an array as an argument which contains all the information about all the touches of the screentouchEnd()
- activates only on touchscreen devices when the screen has been released; it is given an array as an argument which contains all the information about all the touches of the screenmouseDown()
- activates when the left mouse button is clicked; the mouse coordinates can be accessed through the mouseX and mouseY variablesmouseMove()
- activates when the left mouse button is held down and mouse is moving; the mouse coordinates can be accessed through the mouseX and mouseY variablesmouseUp()
- activates when the left mouse button is released; the mouse coordinates can be accessed through the mouseX and mouseY variablesswipe(dir)
- a callback function called when user swipes the screen on a mobile device; it is passed an argument dir which contains the direction of the swipe; dir can either be equal to UP, DOWN, LEFT, or RIGHTcreateVector(x, y)
- returns a Vector object with the given x and y values; if x and y are not given, it returns a zero vectorrandomVector(magnitude)
- returns a random Vector object; magnitude is an optional argument; if provided, the returned vector will have the given magnitude, otherwise, the magnitude is set to 1createPoint(x, y)
- returns a Point object with the given x and y valuesrandomPoint()
- returns a random Point objectcreateCanvas(width, height, canvas)
- returns a Canvas object wit the given width and height values; canvas is an optional argument in case you want to use an existing canvas instead of creating a new one, then you provide the pointer to the canvas you want to useVector.fromAngle(angle)
- returns a Vector object generated from the given anglecolor(red, green, blue, alpha)
- returns a string colorisInArray(array, element)
- returns the element index in the array if the element is indeed in the array, returns false otherwiseramoveFromArray(array, element)
- removes the element from the array if it is indeed in the array, otherwise does nothingcreateMatrix(columns, rows)
- returns a matrix (2D array) with the number of columns and rows filled with null elementsdistance(x1, y1, x2, y2)
- returns the distance between given coordinates; only two arguments can be passed in which case both of them must be either of Vector or Point class and their distance will be returned; you can find the distance between two points, two vectors, or a point and a vectorswap(array, index1, index2)
- swaps the two array elements at the given indexessort(array)
- sorts the arraytoRadians(degrees)
- converts the number of degrees to radians and returns the resulttoDegrees(radians)
- converts the number of radians to degrees and returns the resultconstrain(num, min, max)
- constrains the given number between the min and the max values and returns the valuelerp(value1, value2, step)
- lerp - Linear Interpolation - smooth transitioning between two values with the step givenjoinArray(array, spacing)
- returns a string of all the elements in the array divided by the spacing; spacing is optional, if there is none, it will return the string of all the elements concatenated without spacingremoveChars(text, characters)
- takes two strings as an output; string characters contains all the characters that you want removed from the original string and returns the resultremoveCharAt(text, index)
- removes the character at the given index from the given string and returns the resultreplaceCharAt(text, index, replacement)
- replaces the character at the given index from the given string with the given replacement and returns the result
Class Canvas is the main class of the framework. When an instance is created, it creates a canvas element and appends it to the document body. An instance can be created by calling the createCanvas()
function. Example: let canvas = createCanvas(400, 400);
- width - contains the width of the canvas; it can also be accessed through the width global variable
- height - contains the height of the canvas; it can also be accessed through the height global variable
- maxWidth - maximum canvas width; by default set to 99.000, from -33.000 to + 66.000
- maxHeight - maximum canvas height; by default set to 99.000, from -33.000 to + 66.000
- canvas - points to the actual canvas element that is displayed in the document
- ctx - contains the context of the canvas element
- isPaused - true or false based on whether the canvas is paused or not
setMaxSize(newMaxWidth, newMaxHeight)
- sets the new maximum width and height of the canvas; can take one argument, in which case both maxWidth and maxHeight are set to that valuesetSize(newWidth, newHeight)
- changes the dimensions of the canvas to the new dimensions; also updates the Width and Height global variables; can take one argument, in which case both width and height are set to that valuefullScreen()
- makes the canvas take the whole windowclear()
- clears the whole canvas; called by default at every framebackground(r, g, b)
- changes the canvas background color to the given RGB values; if only one value is passed to the method, that value is taken as the red, green, and blue valuefill(r, g, b, a)
- changes the fill color of the canvas; if only one value is passed to the method, that value is taken as the red, green, and blue value, and alpha is set to 255; to set the alpha value, you must pass all four arguments; alpha ranges from 0 to 255noFill()
- removes the fillstroke(r, g, b, a)
- changes the stroke color of the canvas; if only one value is passed to the method, that value is taken as the red, green, and blue value, and alpha is set to 255; to set the alpha value, you must pass all four arguments; alpha ranges from 0 to 255noStroke()
- removes the strokelineWidth(width)
- changes the stroke line widthline(x1, y1, x2, y2)
- draws a line from (x1, y1) to (x2, y2); lineWidth function affects itlineFromVector(vector)
- draws a line from a vectorlineFromAngle(x, y, angle, length)
- draws a line from an angle in degrees at the given coordinates with the provided lengthrect(x, y, width, height)
- draws a rectangle starting at the given coordinates with the given dimensions; only two arguments can be passed, in which case x and y are set to the first number, and width and height are set to the second numberrectMode(mode)
- sets the drawing mode for rectangles on canvas; takes a string for an input; valid values are CENTER or CORNERpoint(x, y)
- draws a point at the x and y coordinates; the size of the point is changed via the lineWidth methodcircle(x, y, radius)
- draws a circle at the given coordinates with the given radiusellipse(x, y, width, height, rotation)
- draws an ellipse at the given coordinates with the provided dimensions; rotation is an optional argument and it takes a number of degreesarc(x, y, radius, startAngle, endAngle)
- draws an arc at the given coordinates with the given parametersbeginShape(x, y)
- starts drawing a new shape at the given coordinatesvertex(x, y)
- adds another point to the new shape at the given coordinatescloseShape()
- closes the new shapetext(text, x, y, fontSize, fontName)
- displays the given text on the canvas at the given coordinates; fontSize is an integer, fontName is a stringtextAlign(alignment)
- sets the alignment for the displayed text; takes a string for an input; valid values are START, END, LEFT, RIGHT, CENTERtranslate(x, y)
- translates the canvas starting point to the given coordinatesrotate(angle)
- rotates the canvas by the given anglescale(widthScale, heightScale)
- scales the canvas proportionate to the given arguments; if only one argument is given, both width and height are scaled in proportion to that one argument; if no arguments are passed, canvas is returned to scale 1save()
- saves the current state of the canvasrestore()
- restores the last saved state of the canvasscreenshot()
- takes a screenshot of the canvas and saves it as a png fileplayPause()
- switches between states of playin and pausedpause()
- pauses the loop if it is unpausedplay()
- unpauses the loop if it is pauseddrawImage(image, sx, sy, swidth, sheight, x, y, width, height)
- draws an image on the canvas; Arguments:
- image - an image object
- sx (optional) - x value from which the cropping of the image starts
- sy (optional) - y value from which the cropping of the image starts
- swidth (optional) - width of the crop
- sheight (optional) - height of the crop
- x - x coordinate of the canvas from which the image starts drawing
- y - y coordinate of the canvas from which the image starts drawing
- width (optional) - width of the image on the canvas; if left blank, it will be displayed in its full size
- height (optional) - height of the image on the canvas; if left blank, it will be displayed in its full size
Takes a path to the image as an argument.
- path - contains the path to the image
- filename - contains the name of the image file
- image - contains the image element
Takes a path to the audio file as an argument/
- path - contains the path to the audio file
- filename - contains the name of the audio file
- audio - contains the audio element
play()
- starts playing the audiopause()
- pauses the audioplayPause()
- pauses the audio if it is playing, plays it if it is paused
A point on the canvas
- x - the x coordinate of the point
- y - the y coordinate of the point
distance(point2)
- returns the distance to the given pointisOffScreen()
- returns true if the point is off the screen to any side; translating the canvas affects its work
A 2D Vector object
- x - the x coordinate
- y - the y coordinate
- previousX - contains the previous x coordinate of the vector in case it has changed
- previousY - contains the previous y coordinate of the vector in case it has changed
set(newX, newY)
- changes vectors x and y coordinates to the new onesadd(vector2)
- adds the two vectors and changes x and y coordinates to the result onessubtract(vector2)
- subtracts the two vectors and changes x and y coordinates to the result onesmultiply(vector2)
- multiplies the two vectors and changes x and y coordinates to the result onesdivide(vector2)
- divides the two vectors and changes x and y coordinates to the result onesangle()
- returns the angle of the vector in relation to the x axisrotate(angle)
- rotates the vector by the given anglemagintude()
- returns the magnitude (length) of the vectormagnitudeSqr()
- returns the magnitude squaredsetMagnitude(newMagnitude)
- sets the magnitude (length) of the vector to the new onecopy()
- returns a copy of the vectornormalize()
- normalizes the vector (sets its magnitude to 1)distance(vector2)
- returns the distance to the given vectorisOffScreen()
- returns true if the vector is off the screen to any side; translating the canvas affects its worklerp(vector2, step)
- lerp - Linear Interpolation - smooth transitioning between current values to the values of the given vector with the provided stepconstrain(minX, maxX, minY, maxY)
- constrains vector between given values
A color class
- red - red value of the color
- green - green value of the color
- blue - blue value of the color
- alpha - alpha value of the color
randomize(randomizeAlpha)
- returns a new Color object containing a random shade of the original color; randomizeAlpha is an optional argument; if it is set to true; the alpha value of the color will also bi randomizedcolor()
- returns a string containing the color
To create a new PanvasJS project you can clone one of the templates from the examples folder. The offline template is self-contained and all the script files come with it, whereas the other template uses the online version of the file and uses the latest version by default. You can change this by changing the url in the script tag in the index.html file.
You can also implement the files into your existing project by dowloading the script file or adding it through the CDN. The URL for the CDN is as follows:
https://www.leptr.com/panvasjs/version/panvas.js or https://www.leptr.com/panavsjs/version/panvas.min.js
To use the latest version, add the following line of code into your head tag
<script src="https://www.leptr.com/panvasjs/latest/panvas.min.js"></script>
The lowest available version is 2.0.8
The latest version is 2.0.8