ankle gredient

This commit is contained in:
hamid zarghami
2026-07-05 16:18:57 +03:30
parent 368cace143
commit 954ad48486
3 changed files with 32 additions and 17 deletions
@@ -7,6 +7,7 @@ import { useSingleUpload } from '@/pages/uploader/hooks/useUploaderData'
import ColorsImage from '@/assets/images/colors.png' import ColorsImage from '@/assets/images/colors.png'
import ColorPicker from '@/components/ColorPicker' import ColorPicker from '@/components/ColorPicker'
import Select from '@/components/Select' import Select from '@/components/Select'
import { toCssLinearGradient } from '../utils/gradient'
const PRESET_COLORS = [ const PRESET_COLORS = [
'#a8edcf', '#a8edcf',
@@ -322,7 +323,7 @@ const SettingsPanel = () => {
<div <div
className="h-12 rounded-xl border border-border" className="h-12 rounded-xl border border-border"
style={{ style={{
backgroundImage: `linear-gradient(${backgroundGradient.angle}deg, ${backgroundGradient.from} 0%, ${backgroundGradient.to} 100%)`, backgroundImage: toCssLinearGradient(backgroundGradient),
}} }}
/> />
</div> </div>
+12 -7
View File
@@ -25,23 +25,24 @@ const getGradientPoints = (
): { start: Point; end: Point } => { ): { start: Point; end: Point } => {
const safeWidth = Math.max(1, width); const safeWidth = Math.max(1, width);
const safeHeight = Math.max(1, height); const safeHeight = Math.max(1, height);
// App/UI angle: 0° = top→bottom, 90° = left→right (clockwise).
const rad = toRadians(normalizeAngle(angle)); const rad = toRadians(normalizeAngle(angle));
const dx = Math.cos(rad);
const dy = Math.sin(rad);
const half = Math.sqrt(safeWidth * safeWidth + safeHeight * safeHeight) / 2; const half = Math.sqrt(safeWidth * safeWidth + safeHeight * safeHeight) / 2;
const halfX = Math.sin(rad) * half;
const halfY = Math.cos(rad) * half;
if (mode === "centered") { if (mode === "centered") {
return { return {
start: { x: -dx * half, y: -dy * half }, start: { x: -halfX, y: -halfY },
end: { x: dx * half, y: dy * half }, end: { x: halfX, y: halfY },
}; };
} }
const cx = safeWidth / 2; const cx = safeWidth / 2;
const cy = safeHeight / 2; const cy = safeHeight / 2;
return { return {
start: { x: cx - dx * half, y: cy - dy * half }, start: { x: cx - halfX, y: cy - halfY },
end: { x: cx + dx * half, y: cy + dy * half }, end: { x: cx + halfX, y: cy + halfY },
}; };
}; };
@@ -91,7 +92,11 @@ export const getSvgGradientEndpoints = (
}; };
}; };
/** Convert app/UI angle to CSS linear-gradient degrees (0deg = upward in CSS). */
export const toCssGradientAngle = (angle: number) =>
normalizeAngle(180 - normalizeAngle(angle));
export const toCssLinearGradient = (gradient: LinearGradient | undefined) => { export const toCssLinearGradient = (gradient: LinearGradient | undefined) => {
if (!gradient) return undefined; if (!gradient) return undefined;
return `linear-gradient(${normalizeAngle(gradient.angle)}deg, ${gradient.from} 0%, ${gradient.to} 100%)`; return `linear-gradient(${toCssGradientAngle(gradient.angle)}deg, ${gradient.from} 0%, ${gradient.to} 100%)`;
}; };
+18 -9
View File
@@ -549,6 +549,16 @@ const BookPage = memo(forwardRef<HTMLDivElement, BookPageProps>(
const fillColor = obj.fill || '#000000'; const fillColor = obj.fill || '#000000';
const strokeColor = obj.stroke || 'transparent'; const strokeColor = obj.stroke || 'transparent';
const gradientId = `triangle-grad-${obj.id}-${index}${idSuffix}`; const gradientId = `triangle-grad-${obj.id}-${index}${idSuffix}`;
const triangleGradientEndpoints =
obj.fillType === 'gradient' && obj.gradient
? getSvgGradientEndpoints(
obj.gradient,
baseWidth * scale,
baseHeight * scale,
'centered',
{ x: halfSize, y: halfSize },
)
: null;
return ( return (
<svg <svg
@@ -573,25 +583,24 @@ const BookPage = memo(forwardRef<HTMLDivElement, BookPageProps>(
)} )}
viewBox={`0 0 ${size} ${size}`} viewBox={`0 0 ${size} ${size}`}
> >
{obj.fillType === 'gradient' && obj.gradient ? ( {triangleGradientEndpoints ? (
<defs> <defs>
<linearGradient <linearGradient
id={gradientId} id={gradientId}
gradientUnits="userSpaceOnUse" gradientUnits="userSpaceOnUse"
x1="0" x1={triangleGradientEndpoints.x1}
y1="0" y1={triangleGradientEndpoints.y1}
x2={size} x2={triangleGradientEndpoints.x2}
y2={size} y2={triangleGradientEndpoints.y2}
gradientTransform={`rotate(${obj.gradient.angle}, ${size / 2}, ${size / 2})`}
> >
<stop offset="0%" stopColor={obj.gradient.from} /> <stop offset="0%" stopColor={obj.gradient!.from} />
<stop offset="100%" stopColor={obj.gradient.to} /> <stop offset="100%" stopColor={obj.gradient!.to} />
</linearGradient> </linearGradient>
</defs> </defs>
) : null} ) : null}
<polygon <polygon
points={`${topX},${topY} ${bottomLeftX},${bottomY} ${bottomRightX},${bottomY}`} points={`${topX},${topY} ${bottomLeftX},${bottomY} ${bottomRightX},${bottomY}`}
fill={obj.fillType === 'gradient' && obj.gradient ? `url(#${gradientId})` : fillColor} fill={triangleGradientEndpoints ? `url(#${gradientId})` : fillColor}
stroke={strokeColor} stroke={strokeColor}
strokeWidth={strokeWidth} strokeWidth={strokeWidth}
/> />