Aliasing toggle button (#1132)

This commit is contained in:
Vishal
2021-08-26 21:14:20 +05:30
committed by GitHub
parent db1a5138e6
commit 6b08cd2355
4 changed files with 71 additions and 12 deletions

View File

@@ -5,8 +5,10 @@ import './custom-els/PinchZoom';
import './custom-els/TwoUp'; import './custom-els/TwoUp';
import * as style from './style.css'; import * as style from './style.css';
import 'add-css:./style.css'; import 'add-css:./style.css';
import { isSafari, shallowEqual } from '../../util'; import { shallowEqual, isSafari } from '../../util';
import { import {
ToggleAliasingIcon,
ToggleAliasingActiveIcon,
ToggleBackgroundIcon, ToggleBackgroundIcon,
AddIcon, AddIcon,
RemoveIcon, RemoveIcon,
@@ -34,6 +36,7 @@ interface State {
scale: number; scale: number;
editingScale: boolean; editingScale: boolean;
altBackground: boolean; altBackground: boolean;
aliasing: boolean;
} }
const scaleToOpts: ScaleToOpts = { const scaleToOpts: ScaleToOpts = {
@@ -48,6 +51,7 @@ export default class Output extends Component<Props, State> {
scale: 1, scale: 1,
editingScale: false, editingScale: false,
altBackground: false, altBackground: false,
aliasing: false,
}; };
canvasLeft?: HTMLCanvasElement; canvasLeft?: HTMLCanvasElement;
canvasRight?: HTMLCanvasElement; canvasRight?: HTMLCanvasElement;
@@ -144,6 +148,12 @@ export default class Output extends Component<Props, State> {
return props.rightCompressed || (props.source && props.source.preprocessed); return props.rightCompressed || (props.source && props.source.preprocessed);
} }
private toggleAliasing = () => {
this.setState((state) => ({
aliasing: !state.aliasing,
}));
};
private toggleBackground = () => { private toggleBackground = () => {
this.setState({ this.setState({
altBackground: !this.state.altBackground, altBackground: !this.state.altBackground,
@@ -254,7 +264,7 @@ export default class Output extends Component<Props, State> {
render( render(
{ mobileView, leftImgContain, rightImgContain, source }: Props, { mobileView, leftImgContain, rightImgContain, source }: Props,
{ scale, editingScale, altBackground }: State, { scale, editingScale, altBackground, aliasing }: State,
) { ) {
const leftDraw = this.leftDrawable(); const leftDraw = this.leftDrawable();
const rightDraw = this.rightDrawable(); const rightDraw = this.rightDrawable();
@@ -288,7 +298,9 @@ export default class Output extends Component<Props, State> {
ref={linkRef(this, 'pinchZoomLeft')} ref={linkRef(this, 'pinchZoomLeft')}
> >
<canvas <canvas
class={style.pinchTarget} class={`${style.pinchTarget} ${
aliasing ? style.pixelated : ''
}`}
ref={linkRef(this, 'canvasLeft')} ref={linkRef(this, 'canvasLeft')}
width={leftDraw && leftDraw.width} width={leftDraw && leftDraw.width}
height={leftDraw && leftDraw.height} height={leftDraw && leftDraw.height}
@@ -304,7 +316,9 @@ export default class Output extends Component<Props, State> {
ref={linkRef(this, 'pinchZoomRight')} ref={linkRef(this, 'pinchZoomRight')}
> >
<canvas <canvas
class={style.pinchTarget} class={`${style.pinchTarget} ${
aliasing ? style.pixelated : ''
}`}
ref={linkRef(this, 'canvasRight')} ref={linkRef(this, 'canvasRight')}
width={rightDraw && rightDraw.width} width={rightDraw && rightDraw.width}
height={rightDraw && rightDraw.height} height={rightDraw && rightDraw.height}
@@ -348,10 +362,31 @@ export default class Output extends Component<Props, State> {
</button> </button>
</div> </div>
<div class={style.buttonGroup}> <div class={style.buttonGroup}>
<button class={style.firstButton} onClick={this.onRotateClick}> <button
class={style.firstButton}
onClick={this.onRotateClick}
title="Rotate"
>
<RotateIcon /> <RotateIcon />
</button> </button>
<button class={style.lastButton} onClick={this.toggleBackground}> {!isSafari && (
<button
class={style.button}
onClick={this.toggleAliasing}
title="Toggle smoothing"
>
{aliasing ? (
<ToggleAliasingActiveIcon />
) : (
<ToggleAliasingIcon />
)}
</button>
)}
<button
class={style.lastButton}
onClick={this.toggleBackground}
title="Toggle background"
>
{altBackground ? ( {altBackground ? (
<ToggleBackgroundActiveIcon /> <ToggleBackgroundActiveIcon />
) : ( ) : (

View File

@@ -86,8 +86,7 @@
font-size: 1.2rem; font-size: 1.2rem;
cursor: pointer; cursor: pointer;
&:focus { &:focus-visible {
/* box-shadow: 0 0 0 2px var(--hot-pink); */
box-shadow: 0 0 0 2px #fff; box-shadow: 0 0 0 2px #fff;
outline: none; outline: none;
z-index: 1; z-index: 1;
@@ -161,3 +160,8 @@ input.zoom {
pointer-events: auto; pointer-events: auto;
} }
} }
.pixelated {
image-rendering: crisp-edges;
image-rendering: pixelated;
}

View File

@@ -11,6 +11,25 @@ const Icon = (props: preact.JSX.HTMLAttributes) => (
/> />
); );
export const ToggleAliasingIcon = (props: preact.JSX.HTMLAttributes) => (
<Icon {...props}>
<circle
cx="12"
cy="12"
r="8"
fill="none"
stroke="currentColor"
stroke-width="2"
/>
</Icon>
);
export const ToggleAliasingActiveIcon = (props: preact.JSX.HTMLAttributes) => (
<Icon {...props}>
<path d="M12 3h5v2h2v2h2v5h-2V9h-2V7h-2V5h-3V3M21 12v5h-2v2h-2v2h-5v-2h3v-2h2v-2h2v-3h2M12 21H7v-2H5v-2H3v-5h2v3h2v2h2v2h3v2M3 12V7h2V5h2V3h5v2H9v2H7v2H5v3H3" />
</Icon>
);
export const ToggleBackgroundIcon = (props: preact.JSX.HTMLAttributes) => ( export const ToggleBackgroundIcon = (props: preact.JSX.HTMLAttributes) => (
<Icon {...props}> <Icon {...props}>
<path d="M3 13h2v-2H3v2zm0 4h2v-2H3v2zm2 4v-2H3c0 1.1.9 2 2 2zM3 9h2V7H3v2zm12 12h2v-2h-2v2zm4-18H9a2 2 0 0 0-2 2v10c0 1.1.9 2 2 2h10a2 2 0 0 0 2-2V5a2 2 0 0 0-2-2zm0 12H9V5h10v10zm-8 6h2v-2h-2v2zm-4 0h2v-2H7v2z" /> <path d="M3 13h2v-2H3v2zm0 4h2v-2H3v2zm2 4v-2H3c0 1.1.9 2 2 2zM3 9h2V7H3v2zm12 12h2v-2h-2v2zm4-18H9a2 2 0 0 0-2 2v10c0 1.1.9 2 2 2h10a2 2 0 0 0 2-2V5a2 2 0 0 0-2-2zm0 12H9V5h10v10zm-8 6h2v-2h-2v2zm-4 0h2v-2H7v2z" />

View File

@@ -14,6 +14,11 @@
import * as WebCodecs from '../util/web-codecs'; import * as WebCodecs from '../util/web-codecs';
import { drawableToImageData } from './canvas'; import { drawableToImageData } from './canvas';
/** If render engine is Safari */
export const isSafari =
/Safari\//.test(navigator.userAgent) &&
!/Chrom(e|ium)\//.test(navigator.userAgent);
/** /**
* Compare two objects, returning a boolean indicating if * Compare two objects, returning a boolean indicating if
* they have the same properties and strictly equal values. * they have the same properties and strictly equal values.
@@ -295,7 +300,3 @@ export async function abortable<T>(
}), }),
]); ]);
} }
export const isSafari =
/Safari\//.test(navigator.userAgent) &&
!/Chrom(e|ium)\//.test(navigator.userAgent);