-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
word cloud redux: extra.Packer + extra.textDimensions = ulysses.lib.p…
…acker
- Loading branch information
Showing
6 changed files
with
245 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
(ns ulysses.lib.packer | ||
(:require [clojure.walk :refer [keywordize-keys]] | ||
[ulysses.utils :refer [map-subels ptr]])) | ||
|
||
(def jsPacker (.-Packer js/extra)) | ||
|
||
(defn fit | ||
[w h blocks] | ||
(let [p (new jsPacker w h)] | ||
(->> blocks | ||
(sort-by :h) | ||
(reverse) | ||
(clj->js) | ||
(.fit p) | ||
(js->clj) | ||
(keywordize-keys) | ||
(map | ||
(fn [block] | ||
(update block :fit dissoc | ||
:used :right :down :w :h)))))) | ||
|
||
(def jsTextDimensions (.-textDimensions js/extra)) | ||
|
||
(defn text-dimensions | ||
[text options] | ||
(-> text | ||
(jsTextDimensions (clj->js options)) | ||
(js->clj) | ||
(keywordize-keys))) | ||
|
||
(defn fit-words | ||
[w h words] | ||
(let [style-base {:fontFamily "Source Sans Pro" :fontWeight "100"}] | ||
[:div | ||
{:style {:position :relative :width w :height h}} | ||
(->> words | ||
(map | ||
(fn [{:keys [name weight]}] | ||
(let [style (assoc style-base :fontSize (-> weight (* 10) (Math/log) (* 35) (str "px")))] | ||
(-> name | ||
(text-dimensions style) | ||
(update :w | ||
(partial + 8)) | ||
(assoc :style style) | ||
(assoc :weight weight))))) | ||
(fit w h) | ||
(map-subels | ||
(fn [{:keys [w h fit name style weight]}] | ||
[:div.keyword | ||
{:style | ||
(assoc style | ||
:width w | ||
:height h | ||
:left (:x fit) | ||
:top (:y fit)) | ||
:class | ||
(str "weight-" (Math/round (* 100 weight)))} | ||
name])))])) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters