GithubHelp home page GithubHelp logo

omar-sedki / canvas-v2 Goto Github PK

View Code? Open in Web Editor NEW
1.0 1.0 0.0 143 KB

Canvas V2 is a JavaScript library which Extend HTML Canvas to make the use of it more simple.

License: MIT License

JavaScript 67.90% SCSS 2.16% HTML 29.93%
canvas html5 javascript

canvas-v2's Introduction

canvas V2 logo

Canvas V2

Canvas V2 is a JavaScript library which Extend HTML Canvas to make the use of it more simple.

Install

include this script in your HTML file.

<script crossorigin src="https://cdn.jsdelivr.net/gh/omar-sedki/canvas-v2@main/dist/js/canvas-v2.js"></script>

How to use

  • You need to add HTML <canvas> element to your HTML.
 <canvas id="v2" width="600" height="300" style="border:1px solid #000000"></canvas>

<canvas> is an HTML element which can be used to draw graphics via JavaScript. The <canvas> element must have an id attribute so it can be referred to by JavaScript. The width and height attribute is necessary to define the size of the canvas in pixels.

Tip: You can add a border to the canvas using a style attribute.

  • The canvas is blank. To draw on it, a script first needs to access the rendering context.
<script>
var canvas = document.getElementById("v2");
var ctx = canvas.getContext("2d");
</script> 

Get the <canvas> element by calling the document.getElementById() method, then you can access the drawing context using its getContext() method. now you can draw on the canvas.

Draw on the Canvas

draw Simple rectangle.

ctx.fillStyle = "#cbf692";
ctx.fillRect(150, 50, 300, 200); 

The fillStyle property can be a CSS color, a gradient, or a pattern

The fillRect(x,y,width,height) method draws a rectangle, filled with the fillStyle

Full Code Example

<!DOCTYPE html>
<html lang="en">
	<head>
	</head>
	<body>
	 <canvas id="v2" width="600" height="300" style="border:1px solid #000000"></canvas>  
  <script crossorigin src="https://cdn.jsdelivr.net/gh/omar-sedki/canvas-v2@main/dist/js/canvas-v2.js"></script>
  <script>
    var canvas = document.getElementById("v2");
    var ctx = canvas.getContext("2d");
    ctx.fillStyle = "#cbf692";
    ctx.fillRect(150, 50, 300, 200); 
  </script> 
	</body>
</html>

Shapes

Triangle

ctx.triangle( pointA, pointB, pointC ) The triangle() method creates a triangle from 3 points . pointA.x, pointA.y coordinate of first point, pointB.x, pointB.y coordinate of second point, pointC.x, pointC.y coordinate of third point.

ex :

ctx.triangle({x:90,y:50},{x:250,y:150},{x:150,y:250})

Square

ctx.square(x, y, size) The square() method creates a square path whose starting point is at (x, y) and whose size is specified by size.

Rectangle

ctx.rect(x, y, width, height) The rect() method creates a rectangle path whose starting point is at (x, y) and whose size is specified by width, height.

Circle

ctx.circle(x, y, radius) The circle() method creates a circle path whose centered at (x, y) and whose radius is specified by radius. ex :

ctx.circle("50%", "50%", "25%")

Polygon

ctx.polygon(x, y, size, sides ) The polygon() method creates a polygon path whose centered at (x, y) and has sides specified by sides and size of sides is specified by size.

canvas-v2's People

Contributors

omar-sedki avatar

Stargazers

 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.