Skocz do zawartości
  • 👋 Witaj na MPCForum!

    Przeglądasz forum jako gość, co oznacza, że wiele świetnych funkcji jest jeszcze przed Tobą! 😎

    • Pełny dostęp do działów i ukrytych treści
    • Możliwość pisania i odpowiadania w tematach
    • System prywatnych wiadomości
    • Zbieranie reputacji i rozwijanie swojego profilu
    • Członkostwo w jednej z największych społeczności graczy

    👉 Dołączenie zajmie Ci mniej niż minutę – a zyskasz znacznie więcej!

    Zarejestruj się teraz

obracanie i transformacja obrazka z tablic w canvas


MsPatryk

Rekomendowane odpowiedzi

Opublikowano

Witam, chce zrobic coś takiego jak uklad sloneczny tyle ze zdjecia brane z tablicy, jednak po przerobieniu kodu udostępnionego w internecie nie ma planety rysowanej. W czym błąd?

<!DOCTYPE html>
<html>
<body>

<canvas id="canvas" width="300" height="300"></canvas>
Your browser does not support the HTML5 canvas tag.</canvas>

<script>

var sun = new Image();
var moon = new Image();
var earth = new Image();
var images=[];

function init(){
  sun.src = 'https://mdn.mozillademos.org/files/1456/Canvas_sun.png';
  moon.src = 'https://mdn.mozillademos.org/files/1443/Canvas_moon.png';
  earth.src = 'https://mdn.mozillademos.org/files/1429/Canvas_earth.png';

 images.push("https://mdn.mozillademos.org/files/1429/Canvas_earth.png");
}

function draw() {
  var ctx = document.getElementById('canvas').getContext('2d');
 
  ctx.globalCompositeOperation = 'destination-over';
  ctx.clearRect(0,0,300,300); // clear canvas

  ctx.fillStyle = 'rgba(0,0,0,0.4)';
  ctx.strokeStyle = 'rgba(0,153,255,0.4)';
  ctx.save();
  ctx.translate(150,150);

  // Earth
  var time = new Date();
  ctx.rotate( ((2*Math.PI)/6)*time.getSeconds() + ((2*Math.PI)/6000)*time.getMilliseconds() );
  ctx.translate(105,0);
  //ctx.fillRect(0,-12,50,24); // Shadow

    ctx.drawImage(images[0],-12,-12);
 
 // ctx.drawImage(earth,-12,-12);

  // Moon
  ctx.save();
  //ctx.rotate( ((2*Math.PI)/6)*time.getSeconds() + ((2*Math.PI)/6000)*time.getMilliseconds() );
  //ctx.translate(0,28.5);
  //ctx.drawImage(moon,-3.5,-3.5);
  ctx.restore();

  ctx.restore();
 
  ctx.beginPath();
  //ctx.arc(150,150,105,0,Math.PI*2,false); // Earth orbit
  ctx.stroke();
 
  //ctx.drawImage(sun,0,0,300,300);

  window.requestAnimationFrame(draw);
}

init();
</script>

<p><strong>Note:</strong> The canvas tag is not supported in Internet
Explorer 8 and earlier versions.</p>

</body>
</html>
Opublikowano

Zacznijmy od tego, że nie wywołujesz nigdzie funkcji draw();

Po drugie do funkcji drawImage wrzucasz link do zdjęcia a nie utworzone zdjęcie.

<script>
var sun = new Image();
var moon = new Image();
var earth = new Image();
var images = [];

function init(){
  sun.src = 'https://mdn.mozillademos.org/files/1456/Canvas_sun.png';
  moon.src = 'https://mdn.mozillademos.org/files/1443/Canvas_moon.png';
  earth.src = 'https://mdn.mozillademos.org/files/1429/Canvas_earth.png';
  images.push("https://mdn.mozillademos.org/files/1429/Canvas_earth.png");
  draw();
}

function draw() {
  var ctx = document.getElementById('canvas').getContext('2d');
 
  ctx.globalCompositeOperation = 'destination-over';
  ctx.clearRect(0,0,300,300); // clear canvas

  ctx.fillStyle = 'rgba(0,0,0,0.4)';
  ctx.strokeStyle = 'rgba(0,153,255,0.4)';
  ctx.save();
  ctx.translate(150,150);

  // Earth
  var time = new Date();
  ctx.rotate( ((2*Math.PI)/6)*time.getSeconds() + ((2*Math.PI)/6000)*time.getMilliseconds() );
  ctx.translate(105,0);
  //ctx.fillRect(0,-12,50,24); // Shadow
  earth.src = images[0];
  ctx.drawImage(earth,-12,-12);
 
 // ctx.drawImage(earth,-12,-12);

  // Moon
  ctx.save();
  //ctx.rotate( ((2*Math.PI)/6)*time.getSeconds() + ((2*Math.PI)/6000)*time.getMilliseconds() );
  //ctx.translate(0,28.5);
  //ctx.drawImage(moon,-3.5,-3.5);
  ctx.restore();

  ctx.restore();
 
  ctx.beginPath();
  //ctx.arc(150,150,105,0,Math.PI*2,false); // Earth orbit
  ctx.stroke();
 
  //ctx.drawImage(sun,0,0,300,300);

  window.requestAnimationFrame(draw);
}
init();
</script>

"Myśl, myśl, myśl..." - Kubuś Puchatek || "Manual, manual, manual..." - Kubuś Programista


"Szukaj, szukaj, szukaj..." - Kubuś Odkrywca || "Debuguj, debuguj, debuguj..." - Kubuś Developer


 


Potrzebujesz czegoś? Oznacz mnie @Salur

Zarchiwizowany

Ten temat przebywa obecnie w archiwum. Dodawanie nowych odpowiedzi zostało zablokowane.

×
×
  • Dodaj nową pozycję...