Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow the user to use custom shapes and enable/disable the border #42

Merged
merged 15 commits into from
Aug 12, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ packages
dist
.DS_Store
out/
.webpack
.webpack
.idea
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,10 @@ After running for the first time you can access the app settings through the tra
<td>o</td>
<td>Toggle rounded camera (window must be focused)</td>
</tr>
<tr>
<td>p</td>
<td>Toggle <a href="#using-custom-shapes">custom shapes</a> (window must be focused)</td>
</tr>
<tr>
<td>r</td>
<td>Reset zoom (window must be focused)</td>
Expand Down Expand Up @@ -104,8 +108,15 @@ After running for the first time you can access the app settings through the tra
</tbody>
</table>


> On macOS you can use Command instead of Ctrl.

## Using custom shapes

You can use custom shapes using the [`clip-path`](https://developer.mozilla.org/en-US/docs/Web/CSS/clip-path)
CSS property. You can use a tool like [Clippy](https://bennettfeely.com/clippy/) to play around with different shapes
you can build with `clip-path`.

## Author

👤 **Mayk Brito**
Expand Down
8 changes: 8 additions & 0 deletions electron/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,9 @@ const userPreferencesSchema: Schema<unknown> = {
rounded: {
type: JSONSchemaType.Boolean,
},
clipPath: {
type: JSONSchemaType.String,
},
flipHorizontal: {
type: JSONSchemaType.Boolean,
},
Expand All @@ -77,6 +80,9 @@ const userPreferencesSchema: Schema<unknown> = {
borderColorCss: {
type: JSONSchemaType.String,
},
showBorder: {
type: JSONSchemaType.Boolean,
},
}

export const userPreferences = new Store({
Expand Down Expand Up @@ -107,9 +113,11 @@ export const userPreferences = new Store({
hideCamera: 'Shift+Alt+CommandOrControl+3',
},
rounded: true,
clipPath: '',
flipHorizontal: false,
zoom: 1.1,
borderColorCss: 'linear-gradient(to right, #988BC7, #FF79C6)',
showBorder: true,
},
})

Expand Down
42 changes: 25 additions & 17 deletions public/index.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
Expand All @@ -9,9 +10,11 @@
<style>
:root {
--border-color: linear-gradient(to right, #988BC7, #FF79C6);
--clip-path: inherit;
}

body, #video-grid {
body,
#video-grid {
-webkit-app-region: drag;
}

Expand All @@ -27,15 +30,10 @@

video {
height: 100vh;
border-radius: 9999px;
pointer-events: none;
background: #121214;
}

.flip {
transform: rotateY(180deg);
}

#wrapper {
width: 100vw;
height: 100vh;
Expand All @@ -50,49 +48,59 @@
color: #FFF;
background: transparent;
background-clip: padding-box;
border: solid 5px transparent;
border-radius: 16px;
}

#wrapper:before {
content: '';
position: absolute;
top: 0; right: 0; bottom: 0; left: 0;
top: 0;
right: 0;
bottom: 0;
left: 0;
z-index: -1;
margin: -5px;
border-radius: inherit;
background: var(--border-color);
}

.video-wrapper {
overflow: hidden;
display: flex;
align-items: center;
justify-content: center;
border-radius: 16px;
background: transparent;
}

#wrapper.rounded {
border-radius: 50%;
/* fix border-radius on Mac Mojave */
height: calc(100vw - 1px);
#wrapper.has-border {
border: solid 5px transparent;
}

#wrapper.rounded .video-wrapper {
border-radius: 9999px;
#wrapper.has-border:before {
background: var(--border-color);
}

#wrapper.rounded,
#wrapper.rounded .video-wrapper,
#wrapper.rounded video {
clip-path: circle(50% at 50% 50%);
}

#wrapper.has-clip-path:before,
#wrapper.has-clip-path .video-wrapper {
clip-path: var(--clip-path);
}
</style>

<script>
const global = globalThis;
</script>
</head>

<body>
<div id="wrapper">
<div class="video-wrapper">
<video id="video" autoplay muted></video>
</div>
</div>
</body>

</html>
52 changes: 39 additions & 13 deletions src/cam.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,21 @@ type ZoomType = 'in' | 'out'
export class CameraController {
public videoElement: HTMLVideoElement

private videoWrapper: HTMLDivElement
private wrapperElement: HTMLDivElement
private isFlipped: boolean
private isRounded: boolean
private isClipped: boolean
private position: Record<'x' | 'y' | 'z', number>
private root: HTMLElement

constructor() {
this.videoWrapper = document.getElementById('wrapper') as HTMLDivElement
this.wrapperElement = document.getElementById('wrapper') as HTMLDivElement
this.videoElement = document.getElementById('video') as HTMLVideoElement
this.root = document.querySelector(':root') as HTMLElement

this.isFlipped = config.flipHorizontal
this.isRounded = config.rounded
this.isClipped = !!config.clipPath

this.position = {
x: config.horizontal,
Expand Down Expand Up @@ -75,24 +77,48 @@ export class CameraController {
this.render()
}

private render() {
const transform: string[] = []
const classList: string[] = []
public clip() {
this.isClipped = !this.isClipped

transform.push(`translate(${this.position.x}%, ${this.position.y}%)`)
transform.push(`scale(${this.position.z})`)
this.render()
}

private applyPositioning() {
this.videoElement.style.transform = `translate(${this.position.x}%, ${this.position.y}%) scale(${this.position.z})`

if (this.isFlipped) {
classList.push('flip')
transform.push('rotateY(180deg)')
this.videoElement.style.transform += 'rotateY(180deg)'
DouglasdeMoura marked this conversation as resolved.
Show resolved Hide resolved
}
}

this.videoElement.style.transform = transform.join(' ')
this.videoElement.className = classList.join(' ')
this.videoWrapper.className = this.isRounded ? 'rounded' : ''

private applyBorder() {
if (config.borderColorCss) {
this.root.style.setProperty('--border-color', config.borderColorCss)
}

if (config.showBorder) {
this.wrapperElement.classList.add('has-border')
}
}

private applyShape() {
if (this.isRounded) {
this.wrapperElement.classList.add('rounded')
} else {
this.wrapperElement.classList.remove('rounded')
}

if (this.isClipped && config.clipPath) {
this.wrapperElement.classList.add('has-clip-path')
this.root.style.setProperty('--clip-path', config.clipPath)
} else {
this.wrapperElement.classList.remove('has-clip-path')
}
}

private render() {
this.applyPositioning()
this.applyBorder()
this.applyShape()
}
}
2 changes: 2 additions & 0 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ const config = {
horizontal: Number(userPreferences.anchor.x ?? 0),
vertical: Number(userPreferences.anchor.y ?? 0),
borderColorCss: userPreferences.borderColorCss,
showBorder: userPreferences.showBorder,
clipPath: userPreferences.clipPath,
}

export { config }
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ const controls = {
ArrowUp: () => cameraController.adjustOffset('up'),
ArrowDown: () => cameraController.adjustOffset('down'),
o: () => cameraController.round(),
p: () => cameraController.clip(),
r: () => cameraController.reset(),
'=': () => cameraController.zoom('in'),
'-': () => cameraController.zoom('out'),
Expand Down