Skip to content

Commit

Permalink
Day 1 - Canvas and WebGL
Browse files Browse the repository at this point in the history
  • Loading branch information
kshipra-jadav committed Jul 4, 2022
1 parent 5559ca5 commit 138528d
Show file tree
Hide file tree
Showing 7 changed files with 109 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions .idea/Wisflux Internship.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

44 changes: 44 additions & 0 deletions canvas_webgl/gl.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
const canvas = document.querySelector('#gl');
const gl = canvas.getContext('webgl');

if (!gl) {
throw new Error('WebGL not supported');
}
const vertexData = [
0, 1, 0,
1, -1, 0,
-1, -1, 0,
];

const buffer = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, buffer);
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(vertexData), gl.STATIC_DRAW);

const vertexShader = gl.createShader(gl.VERTEX_SHADER);
gl.shaderSource(vertexShader, `
attribute vec3 position;
void main() {
gl_Position = vec4(position, 1);
}
`);
gl.compileShader(vertexShader);

const fragmentShader = gl.createShader(gl.FRAGMENT_SHADER);
gl.shaderSource(fragmentShader, `
void main() {
gl_FragColor = vec4(1, 0, 0, 1);
}
`);
gl.compileShader(fragmentShader);

const program = gl.createProgram();
gl.attachShader(program, vertexShader);
gl.attachShader(program, fragmentShader);
gl.linkProgram(program);

const positionLocation = gl.getAttribLocation(program, `position`);
gl.enableVertexAttribArray(positionLocation);
gl.vertexAttribPointer(positionLocation, 3, gl.FLOAT, false, 0, 0);

gl.useProgram(program);
gl.drawArrays(gl.TRIANGLES, 0, 3);
24 changes: 24 additions & 0 deletions canvas_webgl/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Canvas and WebGL</title>
<script src="gl.js" defer></script>
<script src="native.js" defer></script>
</head>
<body>
<h1>The Empty Triangle is Rendered Through Canvas and Red Triangle is Rendered Through WebGL</h1>
<br><br><br><br>
<canvas id="native"></canvas>
<canvas id="gl"></canvas>

<p>
References :-
<ol>
<li>https://www.educative.io/answers/how-to-draw-triangles-in-html-canvas</li>
<li>https://www.youtube.com/watch?v=zbS4a10xBk8</li>
</ol>
</p>

</body>
</html>
10 changes: 10 additions & 0 deletions canvas_webgl/native.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
const canvas_native = document.querySelector("#native")

const context = canvas_native.getContext("2d")
context.beginPath()
context.moveTo(0, 50)
context.lineTo(50, 0)
context.lineTo(100, 50)
context.lineTo(0, 50)
context.stroke()
context.closePath()

0 comments on commit 138528d

Please sign in to comment.