Stretch a video in p5.js? - p5.js

I have a video playing on top of my p5.js canvas. The aspect ratio of my video is 4:3, but I want to stretch the video so that it always fills the entire window whenever the window is resized, no matter what the aspect ratio of the video is.
When I use video.size(x, y) to try to change the size, the video always maintains its 4:3 aspect ratio, leaving it always pillarboxed/letterboxed.
How can I fix this?

To stretch a video to the size of the window with p5.js:
create a canvas with the size of the window by using createCanvas(windowWidth, windowHeight)
paint the entire canvas with the video by using image(vid, 0, 0, width, height)
resize the canvas when the window resizes by usingresizeCanvas(windowWidth, windowHeight) and windowResized()
You can take a look at the code and the demo here: https://glitch.com/~p5-js-play-video-full-window

Related

MLT Framework. How to add text or image watermark on the center of video and add zoomIn animation to it?

Tried this:
-filter watermark:"welcome.jpg" in=0 out=320 composite.progressive=1 producer.align=centre composite.valign=middle composite.halign=center crop_to_fill=1 composite.geometry="0=0,0:80%x80%:0%;60=0,0:100%x100%:100%;260=0,0:120%x120%:100%;320=0,0:120%x120%:0%"
But it doesn't increase size of watermark, it just moves it to right bottom side...
I don't know of a way to animate a center zoom only using the watermark filter. But it is possible to animate a center zoom using the affine transition. You would need to use two tracks: one with the background video and another with the text to be animated. Here is an example:
melt color:blue in=0 out=320 -track welcome.jpg in=0 out=320 -transition affine valign=middle halign=center scale=1 fill=1 geometry="0=10%,10%:80%x80%:0%;60=0,0:100%x100%:100%;260=-10%,-10%:120%x120%:100%;320=-10%,-10%:120%x120%:0%"
The key is that the position of the watermark is relative to the left corner. So, in addition to animating the size, you also need to animate the x and y position as shown in the example.
Also note that the animation will interpolate the size and position for each frame - but the position is rounded to the nearest pixel. So the motion may not be smooth. That is a known limitation to the animation capabilities in MLT.

Resize proportionally and crop image in any aspect like top right,top left..... on html5canvas [duplicate]

I'm trying to upload images and have them fit into different sized boxes....To give you an idea of what the application does: People upload images and have them printed onto posters.
For example, we'd have a poster size of 8" x 10"(live area) and the full size is 9.5" x 11.5", since the minimum DPI is 100, we typically multiple the 8x10 by 100 = 800x1000.
Here's an image explaining that I have an original image(http://i.imgur.com/Uds9rcZ.jpg) and need it to fit accordingly to the different sizes.
I may need to clarify this a bit, so ask questions if needed.
Thanks.
Canvas's context.drawImage has a version which allows you to scale an image while you are drawing it to the canvas.
If you resize disproportionally (like you do in your example) some of your resized image will fall off the canvas. Then your kitty will look distorted (in your example: stretched vertically)
This sample code resizes proportionally by using only the width. This way your kitty is not stretched.
// calculate how much to scale the resulting image
var originalWidth=16;
var originalHeight=20;
var desiredWidth=20;
var scalingFactor = desiredWidth/originalWidth;
// scale the original size proportionally
var newWidth=originalWidth*scalingFactor;
var newHeight=originalHeight*scalingFactor;
// resize the canvas to fit the desired image size
// Note: canvas is a reference to your html canvas element
canvas.width=newWidth;
canvas.height=newHeight;
// Draw the image to the canvas
// This version of drawImage allows you to scale the original image
// while you are drawing it to the canvas.
context.drawImage(
originalImage,
0,0,originalWidth,originalHeight,
0,0,newWidth,newHeight);
I would like to suggest you use the easyCanvas library to do this. The reason for this is that the built-in drawImage method of context do not scale the image proportionally for you, and it involves a small dose of math to get it right, especially in cases where destination shape differs from original and you want it to cover the whole area.
I made a method in this library to handle situations such as these allowing you to draw the original image proportionally into any shape even if the shape doesn't correspond with the original.
See this demo for an live example.
In essence what you do is to draw your uploaded image into the canvas with this method:
ez.drawImageProp(image, x, y, width, height);
where width and height would be that of destination.
It also has offset parameters so you can move the image around within that shape where an offset of 0.5 is center, 0 is from left and 1 is from right:
ez.drawImageProp(image, x, y, width, height, offsetX, offsetY);
Assuming image is already available all you need to do is:
var ez = new easyCanvas('canvas'); /// provide ID of existing canvas
ez.drawImageProp(image, 0, 0, ez.width, ez.height);
Disclaimer: I am the author of easyCanvas. easyCanvas is open-source (GPL3.0 license).

How to keep sprite border the same size when scaling sprite in Unity?

I've created an image in Photoshop to be used as a sprite in Unity and everything works fine while the sprite is scaled at X: 1; Y: 1.
The problem starts when I scale the image up as the border of the image stretches out with the rest of the image. Is there any way to scale an image from its centre or to ignore the image's border when it's scaled?
Here's the example now that I am able to show it:
The rectangle on top is the original image without being scaled up or down and the rectangle on the bottom is scaled at X:5, Y:0.5 but the borders are stretched.
I think that the borders are stretched because it's part of the image and when it's being scaled, the image (including the borders) is just being stretched.
Is there any way to stretch the sprite image but by ignoring the borders?
Are you trying to scale the image and keep the original ratio?
If so, here are the steps:
Hope this helps. Please let me know if you were trying to do something else.
You can use a sliced sprite. The center of the image is scaled to fit the control rectangle but the borders maintain their sizes regardless of the scaling. Check out the Unity doc here: Unity - Manual: Image

UIImagePickerController - Aligning an Image Guide to a Crop Area

I'm using a UIImagePickerController with an overlay of a transparent white rimmed rectangle as a guide for capturing a small section of the view that the user would be interested in, centered in screen coordinates (320 x 480, portrait).
The problem is, I'm not entirely sure which bits of camera's view (actual photo) the viewfinder is showing, it can't be showing all of it because the image resolution is 3264 x 2448, which is a different width-length ratio to the screen space available, and it's showing fullscreen.
I need to be able to crop the exact image area that is "under" the overlay on the UIImagePickerController view from the actual produced image.
I've tried taking a ratio of the width of the sample divided by the width of the portrait camera view, and then multiplying the width of the image by that and scaling the y-coordinates from that, but the results are incorrect.
How could I try to solve this?

Zooming in on CIImage or NSImage?

I am taking photos from a webcam in my Cocoa application and I would like to zoom in on the centre of the image I receive. I start by receiving a CIImage and eventually save an NSImage.
How would I go about zooming in on either of these objects?
“Zoom” means a couple of things. You'll need at least to crop the image, and you may want to scale up. Or you may want to reserve scaling for display only.
CGImage
To crop it, use a CICrop filter.
To scale it, use either a CILanczosScaleTransform filter or a CIAffineTransform filter.
To crop and scale it, use both filters. Simply pass the output of the crop as the input of the scale.
NSImage
Crop and scale are the same operation here. You'll need to create a new, empty NSImage of the desired size (whether it's the size of the source crop if you won't zoom or an increased size if you will zoom), lock focus on it, draw the crop rectangle from the source image into the bounding rectangle of the destination image, and unlock focus.
If the destination rectangle is not the same size as the source (crop) rectangle, it will scale; if they are the same size, it will simply copy or composite pixel-to-pixel.

Resources