added orx-object-fit
This commit is contained in:
20
orx-object-fit/README.md
Normal file
20
orx-object-fit/README.md
Normal file
@@ -0,0 +1,20 @@
|
|||||||
|
# orx-object-fit
|
||||||
|
|
||||||
|
Fits images in frames with two options, contain and cover. Similar to CSS object-fit (https://www.w3schools.com/css/css3_object-fit.asp)
|
||||||
|
|
||||||
|
## usage
|
||||||
|
|
||||||
|
`objectFit(img: ColorBuffer, x: Double, y: Double, w: Double, h: Double, fitMethod, horizontalPosition:Double, verticalPosition:Double)`
|
||||||
|
|
||||||
|
|
||||||
|
objectFitType
|
||||||
|
- `contain`
|
||||||
|
- `cover`
|
||||||
|
|
||||||
|
horizontal values
|
||||||
|
- left ... right
|
||||||
|
- `-1.0` ... `1.0`
|
||||||
|
|
||||||
|
vertical values
|
||||||
|
- top ... bottom
|
||||||
|
- `-1.0` ... `1.0`
|
||||||
83
orx-object-fit/src/main/kotlin/ObjectFit.kt
Normal file
83
orx-object-fit/src/main/kotlin/ObjectFit.kt
Normal file
@@ -0,0 +1,83 @@
|
|||||||
|
package org.openrndr.extras.objectFit
|
||||||
|
|
||||||
|
import org.openrndr.draw.ColorBuffer
|
||||||
|
import org.openrndr.draw.Drawer
|
||||||
|
import org.openrndr.math.map
|
||||||
|
import org.openrndr.shape.Rectangle
|
||||||
|
|
||||||
|
|
||||||
|
enum class FitMethod {
|
||||||
|
Cover,
|
||||||
|
Contain
|
||||||
|
}
|
||||||
|
|
||||||
|
fun Drawer.objectFit(img: ColorBuffer, x: Double = 0.0, y: Double = 0.0, w: Double, h: Double, fitMethod:FitMethod = FitMethod.Cover, horizontalPosition:Double = 0.0, verticalPosition:Double = 0.0) {
|
||||||
|
val sourceWidth = img.width.toDouble()
|
||||||
|
val sourceHeight = img.height.toDouble()
|
||||||
|
|
||||||
|
var targetX = x
|
||||||
|
var targetY = y
|
||||||
|
|
||||||
|
var targetWidth: Double
|
||||||
|
var targetHeight: Double
|
||||||
|
|
||||||
|
var source = Rectangle(x, y, w, h)
|
||||||
|
var target = Rectangle(x, y, w, h)
|
||||||
|
|
||||||
|
if (fitMethod == FitMethod.Contain) {
|
||||||
|
|
||||||
|
targetWidth = w
|
||||||
|
targetHeight = h
|
||||||
|
|
||||||
|
if (w <= targetWidth) {
|
||||||
|
targetWidth = w
|
||||||
|
targetHeight = (sourceHeight / sourceWidth) * w
|
||||||
|
}
|
||||||
|
|
||||||
|
if (h <= targetHeight) {
|
||||||
|
targetHeight = h
|
||||||
|
targetWidth = (sourceWidth / sourceHeight) * h
|
||||||
|
}
|
||||||
|
|
||||||
|
val left = x
|
||||||
|
val right = x + w - targetWidth
|
||||||
|
val top = y
|
||||||
|
val bottom = y + h - targetHeight
|
||||||
|
|
||||||
|
targetX = map(-1.0, 1.0, left, right, horizontalPosition)
|
||||||
|
targetY = map(-1.0, 1.0, top, bottom, verticalPosition)
|
||||||
|
|
||||||
|
source = Rectangle(0.0, 0.0, sourceWidth, sourceHeight)
|
||||||
|
target = Rectangle(targetX, targetY, targetWidth, targetHeight)
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
if (fitMethod == FitMethod.Cover) {
|
||||||
|
|
||||||
|
targetWidth = sourceWidth
|
||||||
|
targetHeight = sourceHeight
|
||||||
|
|
||||||
|
if (sourceWidth <= targetWidth) {
|
||||||
|
targetWidth = sourceWidth
|
||||||
|
targetHeight = (h / w) * sourceWidth
|
||||||
|
}
|
||||||
|
|
||||||
|
if (sourceHeight <= targetHeight) {
|
||||||
|
targetHeight = sourceHeight
|
||||||
|
targetWidth = (w / h) * sourceHeight
|
||||||
|
}
|
||||||
|
|
||||||
|
val left = 0.0
|
||||||
|
val right = sourceWidth - targetWidth
|
||||||
|
val top = 0.0
|
||||||
|
val bottom = sourceHeight - targetHeight
|
||||||
|
|
||||||
|
targetX = map(-1.0, 1.0, left, right, horizontalPosition)
|
||||||
|
targetY = map(-1.0, 1.0, top, bottom, verticalPosition)
|
||||||
|
|
||||||
|
source = Rectangle(targetX, targetY, targetWidth, targetHeight)
|
||||||
|
target = Rectangle(x, y, w, h)
|
||||||
|
}
|
||||||
|
|
||||||
|
image(img, source, target)
|
||||||
|
}
|
||||||
@@ -28,4 +28,5 @@ include 'orx-camera',
|
|||||||
'orx-kinect-v1-natives-linux-x64',
|
'orx-kinect-v1-natives-linux-x64',
|
||||||
'orx-kinect-v1-natives-macos',
|
'orx-kinect-v1-natives-macos',
|
||||||
'orx-kinect-v1-natives-windows',
|
'orx-kinect-v1-natives-windows',
|
||||||
'orx-kinect-v1-demo'
|
'orx-kinect-v1-demo',
|
||||||
|
'orx-object-fit'
|
||||||
|
|||||||
Reference in New Issue
Block a user