Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

164
Visualizações
trying to draw a rectangle and it is not showing up but there's no error

I've created an animation loop that works fine, but when I created a function to draw a rectangle, nothing shows up. I don't get an error but the rectangle doesn't appear. I did console.log("hello") at the end of the rect function to make sure it works, and "hello" does appear in console, so at this point, I don't know what's wrong. Any help would be greatly appreciated.

Here's my complete code:

///// VARIABLES 

var canvas;
var ctx;
var w = 1000;
var h = 600; 

///// EXECUTABLE CODE 

setUpCanvas();


var o1 = {
    "x": w/3,
    "y": h/3,
    "w": 20,
    "h": 20,
    "c": 180,
    "a": 0.5,
    "angle": 0,
    "changle": 30,
    "distance": 50,

}

var o2 = {
    "x": w/3,
    "y": h/3,
    "c": 280,
    "a": 0.5,
    "w": 0,
    "h": 30, 
}


var oneDegree = 2*Math.PI/360;



setUpCanvas();



animationLoop();

rect(o2);

function animationLoop(){
    clear();
    shape(o1);
    angle(o1,2);
    forward(o1,10);

    requestAnimationFrame(animationLoop);
}

function clear(){
    ctx.clearRect(0,0,w,h);
}


function shape(o){
    var x = o.x;
    var y = o.y;
    var a = o.angle;
    ctx.beginPath();
    ctx.moveTo(o.x,o.y);
    for ( var i=0; i<300; i++){
        angle(o, 10);
        forward(o,2+i*10);
        ctx.lineTo(o.x, o.y);
        ctx.stroke();
    }
    o.x = x;
    o.y = y;
    o.angle = a;
}


function angle(o,a){ 
    // console.log(a);
    if( a == undefined){
        o.angle += o.changle;
    }else{
    o.angle += a;
    }
}


function forward(o,d){
    var cX;
    var cY;

    if(d != undefined){
        o.distance= d;    
    };
        cX = o.distance*Math.cos(o.angle*oneDegree*5);
        cY = o.distance*Math.sin(o.angle*oneDegree*5);
        o.x += cX;
        o.y += cY;
    
}


function rect(o){
    ctx.beginPath();
    ctx.moveTo(o.x , o.y);
    ctx.lineTo(o.x+o.w, o.y);
    ctx.lineTo(o.x+o.w, o.y+o.h);
    ctx.lineTo(o.x, o.y+o.h);
    ctx.lineTo(o.x, o.y);
    ctx.fillStyle = "hsla("+o.c+", 100%, 50%, "+o.a+")";
    ctx.fill();
   
}

//GENERAL STUFF
function randn(r){
    var result = Math.random()*r - r/2;
    return result;
}

function randi(r){
    var result = Math.floor(Math.random()*r);
    return result 
}

function rand(r){
    var result = Math.random()*r;
    return result
}

function setUpCanvas(){
    canvas = document.querySelector("#myCanvas");
    ctx = canvas.getContext("2d");
    canvas.width = w;
    canvas.height = h;
    canvas.style.border="2px solid black";
}

console.log("Assignment 2");
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

I can see the rectangle, I changed the color of the rectangle to red to make it more visible to myself. Your code is correct. Did I miss anything? here is the snippet, it just your code

///// VARIABLES 

var canvas;
var ctx;
var w = 1000;
var h = 600; 

///// EXECUTABLE CODE 

setUpCanvas();

var o1 = {
    "x": w/3,
    "y": h/3,
    "w": 20,
    "h": 20,
    "c": 180,
    "a": 0.5,
    "angle": 0,
    "changle": 30,
    "distance": 50,

}

var o2 = {
    "x": w/3,
    "y": h/3,
    "c": 280,
    "a": 0.5,
    "w": 0,
    "h": 30, 
}


var oneDegree = 2*Math.PI/360;



setUpCanvas();

animationLoop();

rect(o2);
myRect(o2);

function animationLoop(){
    clear();
    shape(o1);
    angle(o1,2);
    forward(o1,10);

    requestAnimationFrame(animationLoop);
}

function clear(){
    ctx.clearRect(0,0,w,h);
}


function shape(o){
    var x = o.x;
    var y = o.y;
    var a = o.angle;
    ctx.beginPath();
    ctx.moveTo(o.x,o.y);
    for ( var i=0; i<300; i++){
        angle(o, 10);
        forward(o,2+i*10);
        ctx.lineTo(o.x, o.y);
        ctx.stroke();
    }
    o.x = x;
    o.y = y;
    o.angle = a;
}


function angle(o,a){ 
    // console.log(a);
    if( a == undefined){
        o.angle += o.changle;
    }else{
    o.angle += a;
    }
}


function forward(o,d){
    var cX;
    var cY;

    if(d != undefined){
        o.distance= d;    
    };
        cX = o.distance*Math.cos(o.angle*oneDegree*5);
        cY = o.distance*Math.sin(o.angle*oneDegree*5);
        o.x += cX;
        o.y += cY;
    
}

function myRect(o){
 
    //ctx.fillStyle = "hsla("+o.c+", 100%, 50%, "+o.a+")";
    
    ctx.beginPath();
    ctx.lineWidth = "2";
    ctx.strokeStyle = "green";
    ctx.rect(o.x, o.y, o.x+o.w, o.y+o.h);
    ctx.stroke();
   
}

function rect(o){
    ctx.beginPath();
    ctx.moveTo(o.x , o.y);
    ctx.lineTo(o.x+o.w, o.y);
    ctx.lineTo(o.x+o.w, o.y+o.h);
    ctx.lineTo(o.x, o.y+o.h);
    ctx.lineTo(o.x, o.y);
    ctx.fillStyle = "hsla("+o.c+", 100%, 50%, "+o.a+")";
    ctx.fill();
   
}

//GENERAL STUFF
function randn(r){
    var result = Math.random()*r - r/2;
    return result;
}

function randi(r){
    var result = Math.floor(Math.random()*r);
    return result 
}

function rand(r){
    var result = Math.random()*r;
    return result
}

function setUpCanvas(){
    canvas = document.querySelector("#myCanvas");
    ctx = canvas.getContext("2d");
    canvas.width = w;
    canvas.height = h;
    canvas.style.border="2px solid red";
}

console.log("Assignment 2");
<canvas id="myCanvas" width="150" height="150"></canvas>

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda