5

I created a new image but I'm trying to resize it and add some transformations to it. How ever, they are not taking effect.

Example: https://jsfiddle.net/chung9ey/3/

img.onload = function(){
    img.style.width = "500";
    img.style.height = "300";
    img.style.transform = "perspective(500px) rotateZ(30deg)";
    context.drawImage(img, 0 ,0);   
}
5
  • 2
    you need a unit on numerical CSS length values other than 0 Commented Jul 6, 2016 at 20:49
  • It should be context.drawImage(img, topLeftCornerHorizontalPosition, topLeftCornerVerticalPosition, imageWidth, imageHeight);. See developer.mozilla.org/en-US/docs/Web/API/… Commented Jul 6, 2016 at 21:02
  • As for adding a rotated image to canvas, it might not be possible using img.style.transform. See Rotate and save image for JavaScript. Commented Jul 6, 2016 at 21:05
  • Here's another way: Drawing image in Canvas at an angle WITHOUT rotating the Canvas Commented Jul 6, 2016 at 21:19
  • Based on that last link, I've written an answer. Commented Jul 6, 2016 at 21:43

4 Answers 4

5

Styles change properties, not attributes. In order to change the actual attributes you would need to use

img.height = 300;

//or by native API
img.setAttribute("height",300);

and so on for each attribute you wanted to change. Note that attributes are part of the html element and not necessarily part of the css definition (more on that here: https://www.w3.org/TR/CSS21/cascade.html#preshint).

Sign up to request clarification or add additional context in comments.

10 Comments

But changing the style properties will make the style changes take effect.
@Barmar - No, not always.
@Barmar: either would work. OP should probably just use his existing routine with correct CSS (fixing the tiny typo), but this answer works because of the default user-agent styling.
This is not working for me either. jsfiddle.net/chung9ey/29 . For some reason, new Image(100, 100) doesn't make it 100 x 100?
@user6432770 - This merely answers the question of how you change the attributes of your image. Perhaps you should rephrase the question or create another which highlights the problem that "is not working" for you, as it is clear to see the attribute changing here.
|
1

Try using this.

document.getElementById("imageID").style.height="300px";
document.getElementById("imageID").style.width="500px";

That should change the element's style width and height to what you want. In the HTML script, it'd be

<img src="source.jog" id="imageID"/>

3 Comments

@dandavis I don't, but it can be changed to what the ID of the image is.
@TravisJ My bad then.
You don't need the id if you have the handle of the image itself (here, img).
1

Based on this Stack Overflow answer, change your JS to:

var img = new Image();
var canvas = document.getElementById("hello");
var context = canvas.getContext("2d");

img.onload = function(){
  context.save(); // Saves current canvas state (includes transformations)
  context.rotate(30); // This rotates canvas context around its top left corner...
  context.translate(-275, 125); // ...so you need to make a correction.
  context.drawImage(img, 0, 0, 500, 300);   
  context.restore(); // Restore canvas state
};

img.src = "https://www.enterprise.com/content/dam/global-vehicle-images/cars/CHRY_200_2015.png";

This essentially rotates the canvas's contents after the image is drawn on it.

Part of the image is off-canvas as a result of this rotation, so you may also want to scale the canvas to fit the rotated image.

https://jsfiddle.net/Lcyksjoz/

Comments

0

i would change the image size in the canvas and then manipulating the canvas through id

var img = new Image(100, 100);
var canvas = document.getElementById("hello");
var context = canvas.getContext("2d");

var width = 500;
var height = 300;
img.onload = function(){
    img.style.transform = "perspective(200px) rotateZ(30deg)";
    context.drawImage(img, 0 ,0, width,height); 
}

img.src = "https://www.enterprise.com/content/dam/global-vehicle-images/cars/CHRY_200_2015.png";

document.getElementById("hello").style.width="300px";

https://jsfiddle.net/chung9ey/27/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.