• We just launched and are currently in beta. Join us as we build and grow the community.

Creating a Rotating Shape Using Javascript

Solier

Silliness Specialist
Divine
S Rep
0
0
0
Rep
0
S Vouches
0
0
0
Vouches
0
Posts
158
Likes
169
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 700 XP
In this chapter, I am going to teach you how to make a rotating image or shape using javascript. Download the source code and follow the directions below. You can use this to your simple projects.

DIRECTIONS

HTML CODE

  1. <!DOCTYPE html>
  2. <html

    >
  3. <head

    >
  4. <meta

    name

    =

    "viewport"

    content

    =

    "width=device-width, initial-scale=1.0"

    /

    >
  5. <style

    >
  6. canvas {
  7. border:1px solid #d3d3d3;
  8. background-color: #f1f1f1;
  9. }
  10. </

    style

    >
  11. </

    head

    >
  12. <body

    onload

    =

    "startGame()"

    >

  13. </

    body

    >
  14. </

    html

    >

  1. <

    script>

  2. var

    myGamePiece;

  3. function

    startGame(

    )

    {
  4. myGamePiece =

    new

    component(

    30

    ,

    30

    ,

    "red"

    ,

    80

    ,

    75

    )

    ;
  5. myGameArea.start

    (

    )

    ;
  6. }

  7. var

    myGameArea =

    {
  8. canvas :

    document.createElement

    (

    "canvas"

    )

    ,
  9. start :

    function

    (

    )

    {
  10. this

    .canvas

    .width

    =

    480

    ;
  11. this

    .canvas

    .height

    =

    270

    ;
  12. this

    .context

    =

    this

    .canvas

    .getContext

    (

    "2d"

    )

    ;
  13. document.body

    .insertBefore

    (

    this

    .canvas

    ,

    document.body

    .childNodes

    [

    0

    ]

    )

    ;
  14. this

    .frameNo

    =

    0

    ;
  15. this

    .interval

    =

    setInterval(

    updateGameArea,

    20

    )

    ;
  16. }

    ,
  17. stop :

    function

    (

    )

    {
  18. clearInterval(

    this

    .interval

    )

    ;
  19. }

    ,
  20. clear :

    function

    (

    )

    {
  21. this

    .context

    .clearRect

    (

    0

    ,

    0

    ,

    this

    .canvas

    .width

    ,

    this

    .canvas

    .height

    )

    ;
  22. }
  23. }

  24. function

    component(

    width,

    height,

    color,

    x,

    y)

    {
  25. this

    .width

    =

    width;
  26. this

    .height

    =

    height;
  27. this

    .angle

    =

    0

    ;
  28. this

    .x

    =

    x;
  29. this

    .y

    =

    y;
  30. this

    .update

    =

    function

    (

    )

    {
  31. ctx =

    myGameArea.context

    ;
  32. ctx.save

    (

    )

    ;
  33. ctx.translate

    (

    this

    .x

    ,

    this

    .y

    )

    ;
  34. ctx.rotate

    (

    this

    .angle

    )

    ;
  35. ctx.fillStyle

    =

    color;
  36. ctx.fillRect

    (

    this

    .width

    /

    -

    2

    ,

    this

    .height

    /

    -

    2

    ,

    this

    .width

    ,

    this

    .height

    )

    ;
  37. ctx.restore

    (

    )

    ;
  38. }
  39. }

  40. function

    updateGameArea(

    )

    {
  41. myGameArea.clear

    (

    )

    ;
  42. myGamePiece.angle

    +=

    1

    *

    Math

    .PI

    /

    180

    ;
  43. myGamePiece.update

    (

    )

    ;
  44. }

  45. </

    script>

Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. After downloading it, you will need a program like Winzip to decompress it.

Virus note: All files are scanned once-a-day by SourceCodester.com for viruses, but new viruses come out every day, so no prevention program can catch 100% of them.

FOR YOUR OWN SAFETY, PLEASE:

1. Re-scan downloaded files using your personal virus checker before using it.

2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.

 

442,401

317,942

317,951

Top