Skip to content

Commit

Permalink
Base
Browse files Browse the repository at this point in the history
  • Loading branch information
aheze committed Sep 26, 2022
1 parent e5b2dfe commit d25ce49
Show file tree
Hide file tree
Showing 6 changed files with 186 additions and 22 deletions.
13 changes: 8 additions & 5 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,15 @@ import PackageDescription

let package = Package(
name: "Prism",
platforms: [
.iOS(.v15),
],
products: [
// Products define the executables and libraries a package produces, and make them visible to other packages.
.library(
name: "Prism",
targets: ["Prism"]),
targets: ["Prism"]
),
],
dependencies: [
// Dependencies declare other packages that this package depends on.
Expand All @@ -20,9 +24,8 @@ let package = Package(
// Targets can depend on other targets in this package, and on products in packages this package depends on.
.target(
name: "Prism",
dependencies: []),
.testTarget(
name: "PrismTests",
dependencies: ["Prism"]),
dependencies: [],
path: "Sources"
),
]
)
6 changes: 0 additions & 6 deletions Sources/Prism/Prism.swift

This file was deleted.

17 changes: 17 additions & 0 deletions Sources/PrismCanvas.swift
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
}
}
86 changes: 86 additions & 0 deletions Sources/PrismEffect.swift
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
)
)
}
}

75 changes: 75 additions & 0 deletions Sources/PrismView.swift
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
}
}
11 changes: 0 additions & 11 deletions Tests/PrismTests/PrismTests.swift

This file was deleted.

0 comments on commit d25ce49

Please sign in to comment.