Is it possible to apply transformations to an image to transform it in a trapezium?
The image initial state is a rectangulum.
The way my scene is build, I have a polygon that allows dragging on the edges, and an image with the same size and position.
I would like the image to have the same shape of the polygon aways, so it would became a trapezium when one polygon axis is dragged. Is this possible?
I assume you're talking about something like an isosceles trapezoid rather than a simple rectangle.
The answer is No, but Yes...
No, not possible with 2D transforms--canvas.getContext("2d"). That's because 2d transforms use a 3x3 matrix that only makes parallel transforms. Since a trapezoid is non-parallel, you cannot possibly transform an image (rectangle) into a trapezoid.
But, Yes...you can use webGL (canvas 3D transforms) to do non-parallel transforms and therefore you can use canvas 3D to convert an image into a trapezoid.
Related
I have a complex shape that's been generated from an SVG file that we're clipping images to in canvas.
However, all of the bezier curve coordinates are based on 0,0. Our shape's size scales depending on the size of the canvas (which scales based on the user's screen width).
Is there a way to take the shape that's already been drawn and then move it to a certain position on the canvas? Much like ctx.scale(#, #) will scale the drawn shape?
Thanks!
Use ctx.translate() to change the position of items on your canvas.
See: https://developer.mozilla.org/en-US/docs/Web/API/Canvas_API/Tutorial/Transformations#Translating
Using translate will alter the position of everything that is subsequently drawn on the canvas. If you just want to move one thing, you can either do:
ctx.translate(x,y);
// draw something
ctx.translate(-x,-y);
or you can do:
ctx.save();
ctx.translate(x,y);
// draw something
ctx.restore();
I am using Raphael to make some manipulations in SVG.
In order to save the SVG as image i use Canvg to render the SVG in canvas.
But the transformations of images inside the SVG rendered in canvas is not right.
Canvg uses native transform() method of html5 to apply the transformations using the below code in line 571 of Canvg.
ctx.transform(this.m[0], this.m[1], this.m[2], this.m[3], this.m[4], this.m[5]);
But this results in transformations like rotations and scaling done around the origin of the image.
But in Raphael the transformations are applied around the center of the image.
So is it possible to apply transformations so everything happens around center of the image?
You can make your own function that makes transformations happen around the center of the image given the image's center points.
The theory around making things transform by its center is as follows :
Translate the image by <-X,-Y,-Z>, where (X,Y,Z) is the image's center.
Do the transformation you want to do
Translate the image by <X,Y,Z>. (You're just returning it back to its original position now)
My objective is to color a rectangle shape in winRT.When the brush leaves rectangle border the coloring should stop.So after coloring I need a fill colored rectangle.
Any help will be really appreciated.
Thanks in advance.
You can use the Clip property to clip any element to a rectangle shape. Note though that right now it only support rectangle clips and for more you would need to use Direct2D or process all the pixels yourself.
When you use Graphics.drawTriangles() and Graphics.beginBitmapFill() to draw two triangles to create a rectangle face, when the face is not square or rectangle, but rather a trapezoid, or an irregular shape, the bitmap used to fill the face is distorted. In order to correct the distortion, Papervision3D has a Boolean property precise. But how does it work?
Thanks! :)
I have two images, a gameboard and the same gameboard with all posible player positions in a pressed state. When a player moves to a position on the board, I put the board image over the pressed board image and slice using context.drawImage() on the pressed image, to display the pressed position through the slice. However, my game board contains positions which are not rectangular but different shapes.
Is it possible with html 5 canvas, to cut a non-rectangular shape out of an image to display the underlying image?
I found it's possible to use clip() on shapes but I can't find a similar option on images.
you can create a third image that is a mask with transparent (alpha-channel) pixels and non-transparent pixels, and use a
globalCompositeOperation
to merge the mask and the to-be-masked image into a new image ( think you can use an xor or source-out here..