<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: noobjonh</title>
    <description>The latest articles on DEV Community by noobjonh (@noobjonh).</description>
    <link>https://dev.to/noobjonh</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F838408%2Fe703b35d-e2cb-448b-86a6-08395e17758f.png</url>
      <title>DEV Community: noobjonh</title>
      <link>https://dev.to/noobjonh</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/noobjonh"/>
    <language>en</language>
    <item>
      <title>portals in javascript games</title>
      <dc:creator>noobjonh</dc:creator>
      <pubDate>Sat, 23 Apr 2022 07:17:56 +0000</pubDate>
      <link>https://dev.to/noobjonh/portals-in-javascript-games-27pm</link>
      <guid>https://dev.to/noobjonh/portals-in-javascript-games-27pm</guid>
      <description>&lt;p&gt;I made a square surface in which all 4 sides are portals to the opposite side just like the 2d representation of a torus surface using only html, css and vanilla javascript.&lt;br&gt;
If you would like  a video representation of the same here is a link to the video:&lt;br&gt;
&lt;a href="https://youtu.be/7f_UdDlyGho"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  &lt;u&gt;the html&lt;/u&gt;
&lt;/h2&gt;

&lt;p&gt;we start by doing a basic setup of the html and adding a html5 canvas tag&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;!DOCTYPE html&amp;gt;
&amp;lt;html lang="en"&amp;gt;
&amp;lt;head&amp;gt;
    &amp;lt;meta charset="UTF-8"&amp;gt;
    &amp;lt;meta http-equiv="X-UA-Compatible" content="IE=edge"&amp;gt;
    &amp;lt;meta name="viewport" content="width=device-width, initial-scale=1.0"&amp;gt;
    &amp;lt;title&amp;gt;TORUS&amp;lt;/title&amp;gt;
    &amp;lt;link rel="stylesheet" href="style.css"&amp;gt;
    &amp;lt;script src="script.js" defer&amp;gt;&amp;lt;/script&amp;gt;
&amp;lt;/head&amp;gt;
&amp;lt;body&amp;gt;
    &amp;lt;canvas id="canvas1"&amp;gt;&amp;lt;/canvas&amp;gt;
&amp;lt;/body&amp;gt;
&amp;lt;/html&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  &lt;u&gt;The css&lt;/u&gt;
&lt;/h2&gt;

&lt;p&gt;We also setup the css by setting the margins, paddings and box sizing.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;*{
    margin:0;
    padding:0;
    box-sizing:border-box;
    font-family: 'Courier New', Courier, monospace;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We also set up  the body to ensure the canvas element is centered&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;body{
    background-color: #c4fdfd4b;
    width: 100vw;
    height: 100vh;
    display:flex;
    justify-content: center;
    align-items: center;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We complete the css setup by setting the canvas element&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;#canvas1{
    background-color: #f7debfa6;
    position: relative;
    height:500px;
    width:700px;
    top:0;
    left:0;
    border:solid black 2px;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  &lt;u&gt;The js&lt;/u&gt;
&lt;/h2&gt;

&lt;h2&gt;
  
  
  canvas sizing
&lt;/h2&gt;

&lt;p&gt;We begin by sizing the canvas by setting its size the same to the height and width written in the css&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const canvas = document.getElementById('canvas1')
const ctx = canvas.getContext('2d')
canvas.width=700
canvas.height=500
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Controls
&lt;/h2&gt;

&lt;p&gt;We set the variables to control the motion of the ball&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let dx = 2
let dy = 2
document.addEventListener('keydown',function(e){
    switch(e.code){
        case 'ArrowUp':
            dy=-2
            break
        case 'ArrowLeft':
            dx=-2
            break
        case 'ArrowDown':
            dy=2
            break
        case 'ArrowRight':
            dx=2
            break
    }
})
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  ball class
&lt;/h2&gt;

&lt;p&gt;The class to make the ball&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Ball{
    constructor(){
        this.radius=50
        this.x=canvas.width/2
        this.y=canvas.height/2
        this.xpos1=this.x
        this.xpos2=this.x
        this.ypos1=this.y
        this.ypos2=this.y
    }
    update(){
        this.x+=dx
        this.y+=dy
        this.xpos1+=dx
        this.xpos2+=dx
        this.ypos1+=dy
        this.ypos2+=dy

        ///////////////////////////////////////
        if(this.x&amp;gt;=canvas.width+this.radius &amp;amp;&amp;amp; dx==2){
            this.x=this.x-canvas.width
        }else if(this.x&amp;lt;=-this.radius &amp;amp;&amp;amp; dx==-2){
            this.x=this.x+canvas.width
        }
        ///////////////////////////////////////
        if(this.y&amp;gt;=canvas.height+this.radius &amp;amp;&amp;amp; dy==2){
            this.y=this.y-canvas.height
        }else if(this.y&amp;lt;=-this.radius &amp;amp;&amp;amp; dy==-2){
            this.y=this.y+canvas.height
        }
    }
    draw(){
        ctx.lineWidth=2
        ctx.strokeStyle='black'
        ctx.fillStyle='green'
        ctx.beginPath()
        ctx.arc(this.x,this.y,this.radius,0,Math.PI*2)
        ctx.stroke()
        ctx.closePath()
        ctx.fill()
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  animate function
&lt;/h2&gt;

&lt;p&gt;This is the function that moves the ball&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let ball1 = new Ball()
function animateMotion(){
    ctx.clearRect(0,0,canvas.width,canvas.height)
    ball1.draw()
    ball1.update()
    requestAnimationFrame(animateMotion)
}
animateMotion()

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  duplicate method
&lt;/h2&gt;

&lt;p&gt;We add a duplicate method to the ball class  to duplicate the ball as it goes through the portal&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Ball{
    constructor(){
        this.radius=50
        this.x=canvas.width/2
        this.y=canvas.height/2
        this.xpos1=this.x
        this.xpos2=this.x
        this.ypos1=this.y
        this.ypos2=this.y
    }
    update(){
        this.x+=dx
        this.y+=dy
        this.xpos1+=dx
        this.xpos2+=dx
        this.ypos1+=dy
        this.ypos2+=dy

        ///////////////////////////////////////
        if(this.x&amp;gt;=canvas.width+this.radius &amp;amp;&amp;amp; dx==2){
            this.x=this.x-canvas.width
        }else if(this.x&amp;lt;=-this.radius &amp;amp;&amp;amp; dx==-2){
            this.x=this.x+canvas.width
        }
        ///////////////////////////////////////
        if(this.y&amp;gt;=canvas.height+this.radius &amp;amp;&amp;amp; dy==2){
            this.y=this.y-canvas.height
        }else if(this.y&amp;lt;=-this.radius &amp;amp;&amp;amp; dy==-2){
            this.y=this.y+canvas.height
        }
    }
    draw(){
        ctx.lineWidth=2
        ctx.strokeStyle='black'
        ctx.fillStyle='green'
        ctx.beginPath()
        ctx.arc(this.x,this.y,this.radius,0,Math.PI*2)
        ctx.stroke()
        ctx.closePath()
        ctx.fill()
    }
duplicate(xcor,ycor){
        ctx.lineWidth=2
        ctx.strokeStyle='black'
        ctx.fillStyle='green'
        ctx.beginPath()
        ctx.arc(xcor,ycor,this.radius,0,Math.PI*2)
        ctx.stroke()
        ctx.closePath()
        ctx.fill()
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  clone tracking
&lt;/h2&gt;

&lt;p&gt;This is to know when the ball is in contact with the portal so it can make the clone. It is added to the update method in the ball class&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Ball{
    constructor(){
        this.radius=50
        this.x=canvas.width/2
        this.y=canvas.height/2
        this.xpos1=this.x
        this.xpos2=this.x
        this.ypos1=this.y
        this.ypos2=this.y
    }
    update(){
        this.x+=dx
        this.y+=dy
        this.xpos1+=dx
        this.xpos2+=dx
        this.ypos1+=dy
        this.ypos2+=dy

        ///////////////////////////////////////
        if(this.x&amp;gt;=canvas.width+this.radius &amp;amp;&amp;amp; dx==2){
            this.x=this.x-canvas.width
        }else if(this.x&amp;lt;=-this.radius &amp;amp;&amp;amp; dx==-2){
            this.x=this.x+canvas.width
        }

        if(this.x&amp;gt;=canvas.width-this.radius){
            this.xpos1=this.x-canvas.width
        }else if(this.x&amp;lt;=this.radius){
            this.xpos2=this.x+canvas.width
        }else{
            this.xpos1=this.x
            this.xpos2=this.x
        }
        ///////////////////////////////////////
        if(this.y&amp;gt;=canvas.height+this.radius &amp;amp;&amp;amp; dy==2){
            this.y=this.y-canvas.height
        }else if(this.y&amp;lt;=-this.radius &amp;amp;&amp;amp; dy==-2){
            this.y=this.y+canvas.height
        }

        if(this.y&amp;gt;=canvas.height-this.radius){
            this.ypos1=this.y-canvas.height
        }else if(this.y&amp;lt;=this.radius){
            this.ypos2=this.y+canvas.height
        }else{
            this.ypos1=this.y
            this.ypos2=this.y
        }
    }
    draw(){
        ctx.lineWidth=2
        ctx.strokeStyle='black'
        ctx.fillStyle='green'
        ctx.beginPath()
        ctx.arc(this.x,this.y,this.radius,0,Math.PI*2)
        ctx.stroke()
        ctx.closePath()
        ctx.fill()
    }
    duplicate(xcor,ycor){
        ctx.lineWidth=2
        ctx.strokeStyle='black'
        ctx.fillStyle='green'
        ctx.beginPath()
        ctx.arc(xcor,ycor,this.radius,0,Math.PI*2)
        ctx.stroke()
        ctx.closePath()
        ctx.fill()
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  clone conditions
&lt;/h2&gt;

&lt;p&gt;This are the conditions that have to be met for the clone to be created.  This is added to the update method.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Ball{
    constructor(){
        this.radius=50
        this.x=canvas.width/2
        this.y=canvas.height/2
        this.xpos1=this.x
        this.xpos2=this.x
        this.ypos1=this.y
        this.ypos2=this.y
    }
    update(){
        this.x+=dx
        this.y+=dy
        this.xpos1+=dx
        this.xpos2+=dx
        this.ypos1+=dy
        this.ypos2+=dy

        ///////////////////////////////////////
        if(this.x&amp;gt;=canvas.width+this.radius &amp;amp;&amp;amp; dx==2){
            this.x=this.x-canvas.width
        }else if(this.x&amp;lt;=-this.radius &amp;amp;&amp;amp; dx==-2){
            this.x=this.x+canvas.width
        }

        if(this.x&amp;gt;=canvas.width-this.radius){
            this.xpos1=this.x-canvas.width
        }else if(this.x&amp;lt;=this.radius){
            this.xpos2=this.x+canvas.width
        }else{
            this.xpos1=this.x
            this.xpos2=this.x
        }
        ///////////////////////////////////////
        if(this.y&amp;gt;=canvas.height+this.radius &amp;amp;&amp;amp; dy==2){
            this.y=this.y-canvas.height
        }else if(this.y&amp;lt;=-this.radius &amp;amp;&amp;amp; dy==-2){
            this.y=this.y+canvas.height
        }

        if(this.y&amp;gt;=canvas.height-this.radius){
            this.ypos1=this.y-canvas.height
        }else if(this.y&amp;lt;=this.radius){
            this.ypos2=this.y+canvas.height
        }else{
            this.ypos1=this.y
            this.ypos2=this.y
        }

        ///////////////////////////////////////////////////////////////////
         if(this.x&amp;gt;=canvas.width-this.radius || this.x&amp;lt;=this.radius || this.y&amp;gt;=canvas.height-this.radius || this.y&amp;lt;=this.radius ){
            this.duplicate(this.xpos1,this.ypos1)
            this.duplicate(this.xpos2,this.ypos2)
            this.duplicate(this.xpos1,this.ypos2)
            this.duplicate(this.xpos2,this.ypos1)
        }
    }
    draw(){
        ctx.lineWidth=2
        ctx.strokeStyle='black'
        ctx.fillStyle='green'
        ctx.beginPath()
        ctx.arc(this.x,this.y,this.radius,0,Math.PI*2)
        ctx.stroke()
        ctx.closePath()
        ctx.fill()
    }
    duplicate(xcor,ycor){
        ctx.lineWidth=2
        ctx.strokeStyle='black'
        ctx.fillStyle='green'
        ctx.beginPath()
        ctx.arc(xcor,ycor,this.radius,0,Math.PI*2)
        ctx.stroke()
        ctx.closePath()
        ctx.fill()
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>tutorial</category>
      <category>gamedev</category>
    </item>
  </channel>
</rss>
