How to set background size cover on a canvas - html5-canvas

I'm using a canvas to make blur effect on boostrap carousel images. Basically, carousel images are set to background size cover so image will crop according to window size. I want to do the same to the canvas image. Any ideas? Thanks heaps!

This snippet should do the trick.
var aspectheight = h * (canvas.width / w);
var heigthoffset = ((aspectheight - canvas.height) / 2) * -1;
ctx.drawImage(image, 0, heigthoffset, canvas.width, aspectheight);
Where h and w are the width and height if the image being rendered, and image is that image.

Related

Unity3d UI issue with Xiaomi

In Xiaomi devices, there are drawn an image outside of camera's letterbox
In other devices everything is correct
I attached both sumsung and xiaomi images, the screenshot that looks ugly is xiaomi, and good look in samsung
float targetaspect = 750f / 1334f;
// determine the game window's current aspect ratio
float windowaspect = (float)Screen.width / (float)Screen.height;
// current viewport height should be scaled by this amount
float scaleheight = windowaspect / targetaspect;
// obtain camera component so we can modify its viewport
Camera camera = GetComponent<Camera>();
// if scaled height is less than current height, add letterbox
if (scaleheight < 1.0f)
{
Rect rect = camera.rect;
rect.width = 1.0f;
rect.height = scaleheight;
rect.x = 0;
rect.y = (1.0f - scaleheight) / 2.0f;
camera.rect = rect;
}
try setting the image to clamp instead of repeat.
this will give the result of black borders but you won't have that weird texture
I don't know what caused that problem, however i solved it in a tricky way. I just added second camera to display black background. Only My main camera's viewport is letterboxed, but not second camera. So it made display to look good

Adjust Image on button

How can I adjust an image to a button in Tkinter?
Actually i have this :
originalImg = Image.open(currentphotofolderPath + file)
img = ImageTk.PhotoImage(originalImg)
Button(photoFrame, image = img, borderwidth=0, height = 200, width = 200)
The problem the image does not adjust to the button with 200x200
I don't want to resize the image with PhotoImage.resize()
The zoom() function should fix your issue:
Return a new PhotoImage with the same image as this widget but zoom it
with X and Y.
Adding the code line below before instantiating the Button() widget should be helpful:
originalImg = Image.open(currentphotofolderPath + file)
originalImg.zoom(200, 200)
img = ImageTk.PhotoImage(originalImg)
Button(photoFrame, image=img, borderwidth=0, height=200, width=200)
you have a couple of choices, the zoom function as posted by Billal, or you create a resize function:
def Resize_Image(image, maxsize):
r1 = image.size[0]/maxsize[0] # width ratio
r2 = image.size[1]/maxsize[1] # height ratio
ratio = max(r1, r2)
newsize = (int(image.size[0]/ratio), int(image.size[1]/ratio))
image = image.resize(newsize, Image.ANTIALIAS)
return image
which will then resize the Image (not the PhotoImage) to the biggest possible size while retaining the aspect ratio (without knowing it beforehand)
note that the resize method should use less memory than the zoom method (if thats an important factor)

Canvas - delete drawimages

In my canvas adds 2 images. 1 images is "background canvas". 2 image is cut from 1 image and add to background.
Add background (1 images):
img.onload = function() {
img2= new Image();
img2.src=e.target.result;
canvas.width = this.width;
canvas.height = this.height;
context.drawImage(img2,0,0);
}
Add 2 images:
var c=document.getElementById('obrazek');
var ctx = c.getContext("2d");
var img1 = new Image();
img1.src =$('#blur').getCanvasImage();
img1.onload = function(){
ctx.translate(xStart,yStart);
ctx.beginPath();
context.arc(0, 0, d, 0, 2 * Math.PI, false);
ctx.clip();
ctx.drawImage(img1,-xStart,-yStart);
}
I need clean background. I want delete 2 image and load again background.
My clean:
context.drawImage(img2,0,0);
After cleaning background, I see:
2 image is wrong removed, why?
You need to clear your clipping region or else all further draws will be restricted to your circle.
save the unclipped context
do your clipped drawing
restore the context to its unclipped state
Like this:
function drawAll(backgroundImage,topImage,xStart,yStart,d){
// clear the whole canvas of all images
ctx.clearRect(0,0,c.width,c.height);
// save the unclipped context
ctx.save()
// draw the background image (not clipped)
ctx.drawImage(backgroundImage,0,0);
// create a clipping circle
ctx.arc(xStart,yStart,d,0,Math.PI*2);
ctx.clip();
// draw the top image restricted to the clipping circle
ctx.drawImage(topImage,xStart-topImage.width/2,yStart-topImage.height/2);
// restore the context (clears the clipping circle)
ctx.restore();
}
The issue is that you need to clear your context before you redraw
See related issue:
How to clear the canvas for redrawing
This too work:
ctx.clearRect(0,0, c.width, c.height);
var w = c.width;
c.width = 1;
c.width = w;
context.clearRect ( 0 , 0 , canvas.width , canvas.height );
context.drawImage(img2,0,0);

Fill & Stoke width issue in html 5 canvas

I want to draw a closed shape(Using paths) & my stroke width is 10.
Now,i want to fill that shape,i can fill it using fill() function of context.
But,when i want to change alpha of my shape,then stroke & fill area overlap at border of shape.
I want only fill the area of shape that remains black after my stroke.
I have attached image of explaining my problem.
Click here to show shape with stork & fill bug.
As you can see in jsfiddle,
-- Color of overlapping area are composite color. That i don't want.
I want it to be exactly same as in border(or stroke color with alpha).
-- i am not enable to specify fill area of closed path.(there is no method of contexx.)
-- I can't use "glabalCompositeOperation",because i am drawing more than 1 shapes in 1 canvas in my application.
The effect you are getting seems to be a property of how canvas draws lines round a shape. Half the thickness of the line is drawn inside the shape and half outside the shape. One way round it is to draw the filled shape and the border as seperate paths. The changes to do this for your example are shown below. This will be more difficult with irregular shapes.
var canvas = document.getElementById("myCanvas");
var context = canvas.getContext("2d");
var topLeftCornerX = 188;
var topLeftCornerY = 50;
var width = 200;
var height = 100;
var linewidth = 10;
context.globalAlpha = 0.5;
context.beginPath();
context.moveTo(topLeftCornerX, topLeftCornerY);
context.lineTo(topLeftCornerX+width,topLeftCornerY);
context.lineTo(topLeftCornerX+width,topLeftCornerY+height);
context.lineTo(topLeftCornerX,topLeftCornerY+height);
context.closePath();
context.fillStyle = "#FF0000";
context.fill();
context.beginPath();
context.moveTo(topLeftCornerX-linewidth/2, topLeftCornerY-linewidth/2);
context.lineTo(topLeftCornerX+width+linewidth/2,topLeftCornerY-linewidth/2);
context.lineTo(topLeftCornerX+width+linewidth/2,topLeftCornerY+height+linewidth/2);
context.lineTo(topLeftCornerX-linewidth/2,topLeftCornerY+height+linewidth/2);
context.closePath();
context.lineWidth = linewidth;
context.strokeStyle = "#00FF00";
context.stroke();

Resize image for Live Tile - WriteableBitmapEx

**
Found the solution
Because of the fact this is a tile, the image will always be strechted to 173 by 173!
To avoid this first create a dummy 173 by 173 and merge this with the resized one!
Rect rect = new Rect(0.0, 0.0, width, height);
WriteableBitmap bitmapDummy = new WriteableBitmap(173, 173);
bitmapDummy.Blit(rect, resized, rect, WriteableBitmapExtensions.BlendMode.None);
**
Well I have created a Background agent to update the live tile of my WP7 app.
But no matter what I try to resize it, I'm not getting a good result!
Any tips? Currently I have following code, but I also tried 135 by 173 and also the other Interpolation.
WriteableBitmap writeableBitmap = new WriteableBitmap(bitmapImage);
var resized = writeableBitmap.Resize(173, 173, System.Windows.Media.Imaging.WriteableBitmapExtensions.Interpolation.Bilinear);
There is also a small rectangle added below to show the title of the app! It's 40px in height, would be great if image would be cropped above.
The actual image is always 250 by 321px
Your problem is that you're not calculating the width/heights to a correct Aspect ratio.
So to get a 1:1 proportions, you would need a width of 134.735 pixels, for a 173 pixel height.
This can be done by first determining what side is the largest
var aspect = Math.Max(bitmapImage.Width, bitmapImage.Height)
var ratio = largest / 173;
var width = width / ratio;
var height = height / ratio;
var resizedImage = writeableBitmap.Resize(width, height, System.Windows.Media.Imaging.WriteableBitmapExtensions.Interpolation.Bilinear);
And remember to set Stretch="Uniform" to avoid stretching the image to unnecessary proportions.
To create a 173x173 pixel image, with the other image applied on top, use the Blit function from WriteableBitmapEx
var tileImage = new WriteableBitmap(173, 173, ...)
tileImage.Blit(new Rect(width, height), resizedImage, new Rect(width, height), BlendMode.None);

Resources