PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Saturday, October 15, 2022

[FIXED] How do I rotate an object with the canvas api but nothing afterward?

 October 15, 2022     canvas, dom, html, javascript     No comments   

Issue

I'm trying to make a cartoon builder app and allow the user to rotate objects but when I'm testing it, it rotates everything behind it.I fixed the roation to 270 degrees for the question, I know how to do this in p5.js and processing.js but I wanna write this in canvas api.Does anyone know if there is something that works similar to p5's pop() and push() functions?

<doctype html>
<html>
<head>
</head>
<body>
<canvas id="canvas" width="400" height="400"></canvas>
<input type="number" placeholder="width" id="width">
<input type="number" placeholder="height" id="height">
</body>
<script>
var canvas=document.getElementById("canvas")
var ctx=canvas.getContext("2d")
ctx.rotate(270);
ctx.fillRect(200,200,200,50);
//line should be flat
ctx.beginPath();
ctx.moveTo(100,100);
ctx.lineTo(150,100);
ctx.closePath();
ctx.stroke();
</script>
<style>
canvas{
background-color:blue;
}
</style>
</html>


Solution

I think you want context.save();and context.restore();



Answered By - Error 2.0
Answer Checked By - Clifford M. (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing