Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
G
graphics_in_c
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container registry
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Sankkara Narayanan
graphics_in_c
Commits
88661766
Commit
88661766
authored
7 years ago
by
opgrsankkar
Browse files
Options
Downloads
Patches
Plain Diff
Add fan and bouncing ball in JavaScript
parent
fb20ee8b
Branches
Branches containing commit
No related tags found
No related merge requests found
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
js/bouncing_ball.html
+38
-0
38 additions, 0 deletions
js/bouncing_ball.html
js/fan.html
+66
-0
66 additions, 0 deletions
js/fan.html
js/graphics.js
+41
-0
41 additions, 0 deletions
js/graphics.js
with
145 additions
and
0 deletions
js/bouncing_ball.html
0 → 100644
+
38
−
0
View file @
88661766
<canvas
id=
"stage"
></canvas>
<script
src=
"graphics.js"
></script>
<script>
(
async
function
()
{
x
=
g
.
getmaxx
()
/
2
;
y
=
g
.
getmaxy
()
/
2
;
up
=
0
;
left
=
0
;
while
(
true
)
{
if
(
y
>=
g
.
getmaxy
()
-
30
)
up
=
1
;
if
(
y
<=
30
)
up
=
0
;
if
(
x
>=
g
.
getmaxx
()
-
30
)
left
=
1
;
if
(
x
<=
30
)
left
=
0
;
g
.
circle
(
x
,
y
,
30
);
/* prepare for next frame */
await
g
.
delay
(
15
);
g
.
cleardevice
();
if
(
up
)
y
=
y
-
5
;
else
y
=
y
+
5
;
if
(
left
)
x
=
x
-
5
;
else
x
=
x
+
5
;
}
})();
</script>
This diff is collapsed.
Click to expand it.
js/fan.html
0 → 100644
+
66
−
0
View file @
88661766
<canvas
id=
"stage"
></canvas>
<script
src=
"graphics.js"
></script>
<script>
function
rotatex
(
x
,
y
,
angle
,
pivotx
,
pivoty
)
{
return
x
*
Math
.
cos
(
angle
)
-
y
*
Math
.
sin
(
angle
)
+
pivotx
*
(
1
-
Math
.
cos
(
angle
))
+
pivoty
*
(
Math
.
sin
(
angle
));
}
function
rotatey
(
x
,
y
,
angle
,
pivotx
,
pivoty
)
{
return
x
*
Math
.
sin
(
angle
)
+
y
*
Math
.
cos
(
angle
)
+
pivoty
*
(
1
-
Math
.
cos
(
angle
))
-
pivotx
*
(
Math
.
sin
(
angle
));
}
(
async
function
()
{
var
cx
,
cy
;
var
l1x1
,
l1y1
,
l1x2
,
l1y2
;
var
l1angle
=
120
/
180.0
*
Math
.
PI
;
var
l2angle
=
240
/
180.0
*
Math
.
PI
;
var
rotate_angle
=
2
/
180.0
*
Math
.
PI
;
var
i
=
0
;
cx
=
g
.
getmaxx
()
/
2
;
cy
=
g
.
getmaxy
()
/
2
;
l1x1
=
cx
;
l1y1
=
cy
-
20
;
l1x2
=
cx
;
l1y2
=
cy
-
90
;
while
(
true
)
{
i
++
;
if
(
i
>=
60
)
{
l1x1
=
cx
;
l1y1
=
cy
-
20
;
l1x2
=
cx
;
l1y2
=
cy
-
90
;
i
=
0
;
}
g
.
circle
(
cx
,
cy
,
20
);
g
.
line
(
l1x1
,
l1y1
,
l1x2
,
l1y2
);
g
.
line
(
rotatex
(
l1x1
,
l1y1
,
l1angle
,
cx
,
cy
),
rotatey
(
l1x1
,
l1y1
,
l1angle
,
cx
,
cy
),
rotatex
(
l1x2
,
l1y2
,
l1angle
,
cx
,
cy
),
rotatey
(
l1x2
,
l1y2
,
l1angle
,
cx
,
cy
)
);
g
.
line
(
rotatex
(
l1x1
,
l1y1
,
l2angle
,
cx
,
cy
),
rotatey
(
l1x1
,
l1y1
,
l2angle
,
cx
,
cy
),
rotatex
(
l1x2
,
l1y2
,
l2angle
,
cx
,
cy
),
rotatey
(
l1x2
,
l1y2
,
l2angle
,
cx
,
cy
)
);
/* prepare for next frame */
await
g
.
delay
(
15
);
g
.
cleardevice
();
l1x1
=
rotatex
(
l1x1
,
l1y1
,
rotate_angle
,
cx
,
cy
);
l1y1
=
rotatey
(
l1x1
,
l1y1
,
rotate_angle
,
cx
,
cy
);
l1x2
=
rotatex
(
l1x2
,
l1y2
,
rotate_angle
,
cx
,
cy
);
l1y2
=
rotatey
(
l1x2
,
l1y2
,
rotate_angle
,
cx
,
cy
);
}
})();
</script>
This diff is collapsed.
Click to expand it.
js/graphics.js
0 → 100644
+
41
−
0
View file @
88661766
document
.
body
.
setAttribute
(
"
style
"
,
"
margin: 0;
"
);
var
canvas
=
document
.
getElementById
(
'
stage
'
);
canvas
.
width
=
window
.
innerWidth
||
document
.
documentElement
.
clientWidth
||
document
.
body
.
clientWidth
;
canvas
.
height
=
window
.
innerHeight
||
document
.
documentElement
.
clientHeight
||
document
.
body
.
clientHeight
;
var
g
=
canvas
.
getContext
(
"
2d
"
);
g
.
getmaxx
=
function
()
{
return
canvas
.
width
;
};
g
.
getmaxy
=
function
()
{
return
canvas
.
height
;
};
g
.
putpixel
=
function
(
x
,
y
)
{
g
.
fillRect
(
x
,
y
,
1
,
1
);
};
g
.
line
=
function
(
x1
,
y1
,
x2
,
y2
)
{
g
.
beginPath
();
g
.
moveTo
(
x1
,
y1
);
g
.
lineTo
(
x2
,
y2
);
g
.
stroke
();
};
g
.
rectangle
=
function
(
x1
,
y1
,
x2
,
y2
)
{
g
.
strokeRect
(
x1
,
y1
,
x2
-
x1
,
y2
-
y1
);
};
g
.
circle
=
function
(
cx
,
cy
,
r
)
{
g
.
beginPath
();
g
.
arc
(
cx
,
cy
,
r
,
0
,
2
*
Math
.
PI
);
g
.
stroke
();
};
g
.
cleardevice
=
function
()
{
g
.
clearRect
(
0
,
0
,
g
.
getmaxx
(),
g
.
getmaxy
());
};
g
.
delay
=
function
(
ms
)
{
return
new
Promise
(
resolve
=>
setTimeout
(
resolve
,
ms
));
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment