|
1 | 1 | "use client"; |
2 | 2 |
|
3 | | -import { useEffect, useMemo, useRef, useState } from "react"; |
| 3 | +import { useEffect, useRef, useState } from "react"; |
4 | 4 | import Link from "next/link"; |
5 | 5 |
|
| 6 | +import WordCloud2, { ListEntry } from "wordcloud"; |
| 7 | + |
6 | 8 | export interface Keyword { |
7 | 9 | word: string; |
8 | 10 | rank: number; |
9 | 11 | usedCnt: number; |
10 | 12 | } |
11 | 13 |
|
12 | | -const minFont = 16; |
13 | | -const maxFont = 48; |
14 | | -const colors = ["#fff", "#A5B4FC", "#818CF8", "#6366F1", "#64748B"]; |
15 | | - |
16 | | -function getFontSize(rank: number) { |
17 | | - return Math.max(maxFont - (rank - 1) * 6, minFont); |
18 | | -} |
19 | | -function getColor(rank: number) { |
20 | | - return colors[rank - 1] || colors[colors.length - 1]; |
21 | | -} |
| 14 | +const colors = ["#6366F1", "#818CF8", "#A5B4FC", "#fff"]; |
22 | 15 |
|
23 | | -interface PlacedKeyword { |
24 | | - word: string; |
25 | | - rank: number; |
26 | | - usedCnt: number; |
27 | | - left: number; |
28 | | - top: number; |
29 | | - fontSize: number; |
30 | | - color: string; |
31 | | - width: number; |
32 | | - height: number; |
33 | | -} |
| 16 | +export default function WordCloud({ keywords }: { keywords: Keyword[] }) { |
| 17 | + const canvasRef = useRef<HTMLCanvasElement>(null); |
| 18 | + const [width, setWidth] = useState(Math.min(Math.max(360, window.innerWidth - 620), 700)); |
34 | 19 |
|
35 | | -function measureText( |
36 | | - text: string, |
37 | | - fontSize: number, |
38 | | - fontWeight = 700 |
39 | | -): { width: number; height: number } { |
40 | | - if (typeof window === "undefined") return { width: 100, height: fontSize }; |
41 | | - const canvas = document.createElement("canvas"); |
42 | | - const ctx = canvas.getContext("2d"); |
43 | | - if (!ctx) return { width: 100, height: fontSize }; |
44 | | - ctx.font = `${fontWeight} ${fontSize}px Pretendard, sans-serif`; |
45 | | - const metrics = ctx.measureText(text); |
46 | | - return { width: metrics.width, height: fontSize }; |
47 | | -} |
| 20 | + const height = 420; |
48 | 21 |
|
49 | | -function isOverlap(a: PlacedKeyword, b: PlacedKeyword) { |
50 | | - return !( |
51 | | - a.left + a.width < b.left || |
52 | | - a.left > b.left + b.width || |
53 | | - a.top + a.height < b.top || |
54 | | - a.top > b.top + b.height |
55 | | - ); |
56 | | -} |
57 | | - |
58 | | -function placeKeywords(keywords: Keyword[], width: number, height: number): PlacedKeyword[] { |
59 | | - const placed: PlacedKeyword[] = []; |
60 | | - const maxTries = 100; |
61 | | - for (const k of keywords) { |
62 | | - const fontSize = getFontSize(k.rank); |
63 | | - const color = getColor(k.rank); |
64 | | - const { width: textWidth, height: textHeight } = measureText(k.word, fontSize); |
65 | | - let left = 0, |
66 | | - top = 0, |
67 | | - tries = 0; |
68 | | - let overlap = false; |
69 | | - do { |
70 | | - const maxLeft = Math.max(width - textWidth, 0); |
71 | | - const maxTop = Math.max(height - textHeight, 0); |
72 | | - left = Math.random() * maxLeft; |
73 | | - top = Math.random() * maxTop; |
74 | | - overlap = placed.some((p) => |
75 | | - isOverlap( |
76 | | - { |
77 | | - ...p, |
78 | | - left, |
79 | | - top, |
80 | | - width: textWidth, |
81 | | - height: textHeight, |
82 | | - word: k.word, |
83 | | - rank: k.rank, |
84 | | - fontSize, |
85 | | - color, |
86 | | - usedCnt: k.usedCnt, |
87 | | - } as PlacedKeyword, |
88 | | - p |
89 | | - ) |
90 | | - ); |
91 | | - tries++; |
92 | | - } while (overlap && tries < maxTries); |
93 | | - placed.push({ |
94 | | - word: k.word, |
95 | | - rank: k.rank, |
96 | | - usedCnt: k.usedCnt, |
97 | | - left, |
98 | | - top, |
99 | | - fontSize, |
100 | | - color, |
101 | | - width: textWidth, |
102 | | - height: textHeight, |
| 22 | + useEffect(() => { |
| 23 | + const cloudKeywords = keywords.map((k) => [k.word, k.usedCnt]) as ListEntry[]; |
| 24 | + WordCloud2(canvasRef.current!, { |
| 25 | + list: cloudKeywords, |
| 26 | + shape: "circle", |
| 27 | + minRotation: 0, |
| 28 | + maxRotation: 0, |
| 29 | + shrinkToFit: true, |
| 30 | + minSize: 14, |
| 31 | + backgroundColor: "#00000000", |
| 32 | + weightFactor: (size) => size * 10, |
| 33 | + color: (word) => { |
| 34 | + const idx = cloudKeywords.findIndex((k) => k[0] === word); |
| 35 | + const colorIdx = idx > 3 ? 3 : idx; |
| 36 | + return colors[colorIdx]; |
| 37 | + }, |
| 38 | + click: (item) => { |
| 39 | + window.open(`https://www.google.com/search?q=${item[0]}`, "_blank"); |
| 40 | + }, |
103 | 41 | }); |
104 | | - } |
105 | | - return placed; |
106 | | -} |
107 | | - |
108 | | -export default function WordCloud({ keywords }: { keywords: Keyword[] }) { |
109 | | - const [isClient, setIsClient] = useState(false); |
110 | | - const [size, setSize] = useState({ width: 900, height: 420 }); |
111 | | - const containerRef = useRef<HTMLDivElement>(null); |
112 | 42 |
|
113 | | - useEffect(() => { |
114 | | - setIsClient(true); |
115 | | - if (!containerRef.current) return; |
116 | | - const handleResize = () => { |
117 | | - if (containerRef.current) { |
118 | | - setSize({ |
119 | | - width: containerRef.current.offsetWidth, |
120 | | - height: containerRef.current.offsetHeight, |
121 | | - }); |
122 | | - } |
| 43 | + const onResize = () => { |
| 44 | + setWidth(Math.min(Math.max(360, window.innerWidth - 620), 700)); |
123 | 45 | }; |
124 | | - handleResize(); |
125 | | - if (typeof window !== "undefined" && "ResizeObserver" in window) { |
126 | | - const observer = new window.ResizeObserver(handleResize); |
127 | | - observer.observe(containerRef.current); |
128 | | - return () => observer.disconnect(); |
129 | | - } |
130 | | - }, []); |
131 | | - |
132 | | - const placedKeywords = useMemo(() => { |
133 | | - if (!isClient) return []; |
134 | | - return placeKeywords(keywords, size.width, size.height); |
135 | | - }, [isClient, keywords, size]); |
136 | | - |
137 | | - if (!isClient) return null; |
| 46 | + window.addEventListener("resize", onResize); |
| 47 | + return () => { |
| 48 | + WordCloud2.stop(); |
| 49 | + window.removeEventListener("resize", onResize); |
| 50 | + }; |
| 51 | + }, [keywords, width]); |
138 | 52 |
|
139 | 53 | return ( |
140 | | - <div |
141 | | - ref={containerRef} |
142 | | - className="relative mx-auto flex h-full w-full flex-row-reverse rounded-lg bg-gray7/30 px-20 py-16" |
143 | | - > |
144 | | - {placedKeywords.map((k) => ( |
145 | | - <Link |
146 | | - key={k.word} |
147 | | - target="_blank" |
148 | | - href={`https://www.google.com/search?q=${k.word}`} |
149 | | - className="absolute flex cursor-pointer select-none items-center gap-1 whitespace-nowrap font-bold transition-transform duration-150" |
150 | | - style={{ |
151 | | - left: k.left, |
152 | | - top: k.top, |
153 | | - fontSize: k.fontSize, |
154 | | - color: k.color, |
155 | | - textShadow: "0 2px 8px rgba(0,0,0,0.15)", |
156 | | - }} |
157 | | - > |
158 | | - {k.word} |
159 | | - </Link> |
160 | | - ))} |
161 | | - <ul className="flex flex-col flex-wrap text-white"> |
162 | | - {placedKeywords.slice(0, 10).map((k) => ( |
| 54 | + <div className="semi-md:flex-row semi-md:items-start semi-md:px-20 mx-auto flex h-full w-full flex-col items-center justify-between gap-5 rounded-lg bg-gray7/30 px-5 py-16"> |
| 55 | + <div style={{ width, height }}> |
| 56 | + <canvas ref={canvasRef} width={width} height={height} /> |
| 57 | + </div> |
| 58 | + <ul className="flex w-full max-w-80 flex-col flex-wrap text-white md:ml-8"> |
| 59 | + {keywords.slice(0, 10).map((k) => ( |
163 | 60 | <li |
164 | 61 | key={`trend-keyword-${k.word}`} |
165 | | - className="flex w-80 items-center justify-between gap-5 border-b border-b-gray7 px-4 py-2.5" |
| 62 | + className="flex items-center justify-between gap-5 border-b border-b-gray7 px-4 py-2.5" |
166 | 63 | > |
167 | 64 | <Link |
168 | 65 | target="_blank" |
|
0 commit comments