-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
186 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
// | ||
// PrismCanvas.swift | ||
// Prism | ||
// | ||
// Created by A. Zheng (github.com/aheze) on 9/26/22. | ||
// Copyright © 2022 A. Zheng. All rights reserved. | ||
// | ||
|
||
|
||
import SwiftUI | ||
|
||
struct PrismCanvas<Content: View>: View { | ||
@ViewBuilder let content: Content | ||
var body: some View { | ||
content | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
// | ||
// PrismEffect.swift | ||
// Prism | ||
// | ||
// Created by A. Zheng (github.com/aheze) on 9/26/22. | ||
// Copyright © 2022 A. Zheng. All rights reserved. | ||
// | ||
|
||
|
||
import SwiftUI | ||
|
||
extension View { | ||
func tiltLeft(tilt: CGFloat) -> some View { | ||
modifier( | ||
PrismLeftEffect(tilt: tilt) | ||
) | ||
} | ||
|
||
func tiltRight(tilt: CGFloat) -> some View { | ||
modifier( | ||
PrismLeftEffect(tilt: tilt) | ||
) | ||
} | ||
|
||
func tiltContent(tilt: CGFloat) -> some View { | ||
modifier( | ||
PrismContentEffect(tilt: tilt) | ||
) | ||
} | ||
} | ||
|
||
struct PrismLeftEffect: GeometryEffect { | ||
var tilt: CGFloat | ||
|
||
var animatableData: CGFloat { | ||
get { tilt } | ||
set { tilt = newValue } | ||
} | ||
|
||
func effectValue(size: CGSize) -> ProjectionTransform { | ||
return ProjectionTransform( | ||
CGAffineTransform(a: 1, b: 0, c: tilt, d: 1, tx: 0, ty: 0) | ||
) | ||
} | ||
} | ||
|
||
struct PrismRightEffect: GeometryEffect { | ||
var tilt: CGFloat | ||
|
||
var animatableData: CGFloat { | ||
get { tilt } | ||
set { tilt = newValue } | ||
} | ||
|
||
func effectValue(size: CGSize) -> ProjectionTransform { | ||
return ProjectionTransform( | ||
CGAffineTransform(a: tilt, b: tilt + (1 - tilt), c: 0, d: 1, tx: 0, ty: 0) | ||
) | ||
} | ||
} | ||
|
||
struct PrismContentEffect: GeometryEffect { | ||
var tilt: CGFloat | ||
|
||
var animatableData: CGFloat { | ||
get { tilt } | ||
set { tilt = newValue } | ||
} | ||
|
||
func effectValue(size: CGSize) -> ProjectionTransform { | ||
let tx: CGFloat = size.height * tilt / 2 | ||
let ty: CGFloat = size.width * tilt / 2 | ||
|
||
return ProjectionTransform( | ||
CGAffineTransform( | ||
a: 1, | ||
b: tilt, | ||
c: -tilt, | ||
d: 1, | ||
tx: tx, | ||
ty: -ty | ||
) | ||
) | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
// | ||
// PrismView.swift | ||
// Prism | ||
// | ||
// Created by A. Zheng (github.com/aheze) on 9/26/22. | ||
// Copyright © 2022 A. Zheng. All rights reserved. | ||
// | ||
|
||
import SwiftUI | ||
|
||
struct PrismView<Content: View, Left: View, Right: View>: View { | ||
var size: CGSize | ||
var tilt: CGFloat | ||
var extrusion: CGFloat | ||
var levitation = CGFloat(0) | ||
var shadowColor = Color.black | ||
var shadowOpacity = CGFloat(0.25) | ||
|
||
@ViewBuilder let content: Content | ||
@ViewBuilder let left: Left | ||
@ViewBuilder let right: Right | ||
|
||
var body: some View { | ||
let topRightOffset = tilt * size.width | ||
let topAngle = atan2(topRightOffset, size.width) | ||
|
||
let extrusionOffset: CGFloat = { | ||
let extrusionOffset = extrusion | ||
return extrusionOffset | ||
}() | ||
|
||
let levitationXOffset = sin(topAngle) * levitation | ||
let levitationYOffset = cos(topAngle) * levitation | ||
let extrusionXOffset = sin(topAngle) * extrusionOffset | ||
let extrusionYOffset = cos(topAngle) * extrusionOffset | ||
|
||
content | ||
.frame(width: size.width, height: size.height) | ||
.background(alignment: .bottom) { | ||
left | ||
.frame(width: size.width, height: extrusionYOffset) | ||
.tiltLeft(tilt: tilt) | ||
.offset(y: extrusionYOffset) | ||
} | ||
.background(alignment: .trailing) { | ||
Color.clear | ||
.frame(width: extrusionYOffset, height: size.height) | ||
.overlay( | ||
right | ||
.frame(width: size.height, height: extrusionYOffset) | ||
.rotationEffect(.degrees(-90)) | ||
) | ||
.tiltRight(tilt: tilt) | ||
.offset(x: extrusionYOffset) | ||
} | ||
.background( | ||
shadowColor | ||
.shadow( | ||
color: shadowColor, | ||
radius: 16, | ||
x: 0, | ||
y: 0 | ||
) | ||
.offset( | ||
x: levitationXOffset + extrusionXOffset + 10, | ||
y: levitationYOffset + extrusionYOffset + 10 | ||
) | ||
.opacity(shadowOpacity) | ||
.opacity(CGFloat(1) - (CGFloat(levitation) / 400)) | ||
.blur(radius: 10 + CGFloat(levitation) / 12) | ||
) | ||
.offset(x: -levitationXOffset, y: -levitationYOffset) /// z height effect | ||
.offset(x: -extrusionXOffset, y: -extrusionYOffset) /// extrusion effect | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.