
Security News
Meet Socket at Black Hat and DEF CON 2025 in Las Vegas
Meet Socket at Black Hat & DEF CON 2025 for 1:1s, insider security talks at Allegiant Stadium, and a private dinner with top minds in software supply chain security.
A lightweight Canvas JavaScript library.
//Initialize the canvas element
var canvas = new cvjs({ id: 'my-canvas', width: 300, height: 300 });
//Draw a new arc
canvas.Arc({ x: 150, y: 150, radius: 100, start: 0, end: 3*Math.PI/2 });
//Set background color
canvas.Fill({ color: '#000000', opacity: 0.5 });
npm install cvjs
.bower install cvjs
.Add to your website:
<script type="text/javascript" src="/path/to/cv.js"></script>
Initialize your canvas element with:
var canvas = new cvjs({ id: 'your-canvas-id', width: 300, height: 300 });
The argument must be an object with the next keys:
id
: ID of your canvas element (mandatory).width
: width dimension of your canvas (in pixels) (mandatory).height
: height dimension of your canvas (in pixels) (mandatory).Use this function for set the width of your canvas element. If no argument is provided, the function returns the actual canvas width.
//Set the canvas width to 500px
canvas.Width(500);
//Get the canvas width
var width = canvas.Width(); //now width is 500
Use this function for set the height of your canvas element. If no argument is provided, the function return the actual canvas height.
//Set the canvas height to 600px
canvas.Height(600);
//Get the canvas height
var height = canvas.Height(); //Now height is 600
Clear a rectangle of your canvas. The argument must be an object with the next keys:
x
: position x of your rectangle.y
: position y of your rectangle.width
: rectangle width.height
: rectangle height.If no argument is provided, the function will clear all the canvas.
//Clear only a rectangle
canvas.Clear({ x: 50, y: 50, width: 100, height: 100 });
//Clear all the canvas
canvas.Clear();
Sets the color used to fill the drawing. The argument can be an object with the following keys:
color
: a color value that indicates the fill color.opacity
: sets the transparency of the fill.//Set the fill of the object
canvas.Fill({ color: '#abcdef', opacity: 0.6 });
The Fill
method also works with a string argument:
//Set only the fill color
canvas.Fill('#abcdef');
The Stroke
method draws the path that you have previous defined. The argument can be an object with the following keys:
color
: color value for the line.width
: sets the line width.cap
: sets the cap end style for the lines. Available values: butt
, round
or square
.join
: sets the corner style. Available values: bevel
, round
or miter
.opacity
: sets the transparency value of the line.//Set the stroke style
canvas.Stroke({ color: '#000000', width: 5, cap: 'round', join: 'round', opacity: 0.5 });
Also, the Stroke
method allows a string argument for set only the stroke color:
//Set only the stroke color
canvas.Stroke('#000000');
Draws a rectangle defined by:
x
: x position of the rectangle.y
: y position of the rectangle.width
: rectangle width.height
: rectangle height.radius
: sets the corner radius of the rectangle (optional).//Draw a normal rectangle
canvas.Rect({ x: 50, y: 50, width: 100, height: 100 });
//Fill the normal rectangle
canvas.Fill('#ed321f');
//Draw a rounded rectangle
canvas.Rect({ x: 200, y: 50, width: 100, height: 100, radius: 20 });
//Fill the rounded rectangle
canvas.Fill('#588bfe');
Draws a line defined by an array of points.
//Draw a single line
canvas.Line([[50, 50], [100, 100]]);
//Set the stroke of the single line
canvas.Stroke('#ed321f');
//Draw five connected lines
canvas.Line([[50, 50], [100, 100], [150, 50], [200, 100], [250, 50], [300, 100]]);
//Set the stroke of the five lines
canvas.Stroke('#588bfe');
Draws a polygon defined by an array of points.
//Draw a triangle
canvas.Polygon([[50, 50], [50, 100], [100, 100]]);
//Fill the friangle
canvas.Fill('#000000');
Draws an arc or curve. The argument must be an object with the following keys:
x
: x-position of the arc center.y
: y-position of the arc center.radius
: arc radius.start
: start angle of the arc, in radians.end
: end angle of the arc, in radians.dir
: sets the direction of the arc (optional). 1
indicates clockwise (default), and -1
indicates counter-clockwise.//Draw a new arc
canvas.Arc({ x: 150, y: 150, radius: 100, start: 0, end: 3*Math.PI/2 });
//Arc fill
canvas.Fill('#abcdef');
Draws a full circle defined by:
x
: x-position of the circle center.y
: y-position of the circle center.radius
: circle radius.//Draw a circle
canvas.Circle({ x: 150, y: 150, radius: 100 });
//Fill the circle
canvas.Fill('#abcdef');
Draws a text on the canvas. The argument of this method must be an object with the following keys:
text
: text to be written on the canvas.color
: text color.x
: x-position of the text.y
: y-position of the text.font
: text font. Default is Arial
.size
: text size in pixels. Default is 16px
.base
: sets the text baseline. Available values: alphabetic
, top
, hanging
, middle
, ideographic
or bottom
. Default is alphabetic
.align
: sets the text align. Available values: center
, end
, left
, right
or start
. Default is start
.mwidth
: sets the maximum allowed width of the text in pixels (optional).opacity
: sets the transparency of the text.//Draw Hello World
canvas.Text({ text: 'Hello World', size: 14, x: 50, y: 50, color: '#588bfe' });
Draws an image on the canvas. You must provide the next information:
img
: sets the image for draw.x
: x-position of the image.y
: y-position of the image.width
: image width (optional, default is the image original width).height
: image height (optiona, default is the image original height).rotate
: rotation angle for the image, in radians (optional).opacity
: sets the transparency of the image.//Draw an image
canvas.Image({ img: imgID, x: 50, y: 50 });
You can use the ctx
method provided for draw directly on the canvas using the canvas API (see http://www.w3schools.com/tags/ref_canvas.asp for get more info). This allows you to use moveTo()
, quadraticCurveTo()
, bezierCurveTo()
, and more!
//Initialize the canvas
var canvas = new cvjs({ id: 'my-canvas', width: 500, height: 500 });
//Move to our new point
canvas.ctx.moveTo(50,50);
//Draw a quadratic curve
canvas.ctx.quadraticCurveTo(50,200,300,50);
Pull requests are always welcome :)
FAQs
A lightweight Canvas JavaScript library.
The npm package cvjs receives a total of 3 weekly downloads. As such, cvjs popularity was classified as not popular.
We found that cvjs demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Meet Socket at Black Hat & DEF CON 2025 for 1:1s, insider security talks at Allegiant Stadium, and a private dinner with top minds in software supply chain security.
Security News
CAI is a new open source AI framework that automates penetration testing tasks like scanning and exploitation up to 3,600× faster than humans.
Security News
Deno 2.4 brings back bundling, improves dependency updates and telemetry, and makes the runtime more practical for real-world JavaScript projects.