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

OJS globe Demo #40

Merged
merged 20 commits into from
Jul 30, 2024
Merged
Show file tree
Hide file tree
Changes from 13 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
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,7 @@ package.json
package-lock.json

/.luarc.json

# temporarily ignoring the _extensions folder within demos/ and tests/ until we organise them into a website
demos/*/_extensions
tests/*/_extensions
31 changes: 21 additions & 10 deletions _extensions/closeread/scroller-init.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,15 @@ document.addEventListener("DOMContentLoaded", () => {
// define an ojs variable if the connector module is available
let focusedSticky = "none";
const ojsModule = window._ojs?.ojsConnector?.mainModule
const ojsScrollerName = ojsModule?.variable();
const ojsScrollerProgress = ojsModule?.variable();
const ojsScrollerDirection = ojsModule?.variable();
ojsScrollerName?.define("crScrollerName", focusedSticky);
ojsScrollerProgress?.define("crScrollerProgress", 0);
ojsScrollerDirection?.define("crScrollerDirection", null);
const ojsTriggerIndex = ojsModule?.variable()
const ojsStickyName = ojsModule?.variable()
const ojsScrollProgress = ojsModule?.variable()
const ojsScrollDirection = ojsModule?.variable()

ojsTriggerIndex?.define("crTriggerIndex", 0);
ojsStickyName?.define("crStickyName", focusedSticky);
ojsScrollProgress?.define("crScrollProgress", 0);
ojsScrollDirection?.define("crScrollDirection", null);
if (ojsModule === undefined) {
console.error("Warning: Quarto OJS module not found")
}
Expand All @@ -44,8 +47,11 @@ document.addEventListener("DOMContentLoaded", () => {
.onStepEnter((response) => {

if (response.direction == "down") {
// update ojs variables
ojsTriggerIndex?.define("crTriggerIndex", response.index);
ojsScrollProgress?.define("crScrollProgress", 0);
focusedStickyName = "cr-" + response.element.getAttribute("data-focus-on");
ojsScrollerName?.define("crScrollerName", focusedStickyName);
ojsStickyName?.define("crStickyName", focusedStickyName);

// applyFocusOn
allStickies.forEach(node => {node.classList.remove("cr-active")});
Expand All @@ -59,8 +65,13 @@ document.addEventListener("DOMContentLoaded", () => {
.onStepExit((response) => {

if (response.direction == "up") {
// update ojs variables
ojsTriggerIndex?.define(
"crTriggerIndex",
response.index - 1 == -1 ? null : response.index - 1);
ojsScrollProgress?.define("crScrollProgress", 1);
focusedStickyName = "cr-" + response.element.getAttribute("data-focus-on");
ojsScrollerName?.define("crScrollerName", focusedStickyName);
ojsStickyName?.define("crStickyName", focusedStickyName);

// applyFocusOn
allStickies.forEach(node => {node.classList.remove("cr-active")});
Expand All @@ -74,9 +85,9 @@ document.addEventListener("DOMContentLoaded", () => {
})
.onStepProgress((response) => {
// { element, index, progress }
ojsScrollerProgress?.define("crScrollerProgress",
ojsScrollProgress?.define("crScrollProgress",
response.progress);
ojsScrollerDirection?.define("crScrollerDirection",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like there's some differences here between using scroll and scroller in the name. What about using the name of whatever we settle on: "trigger" or "step"? (we can discuss this in our meeting!)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mmm, yeah! I currently have:

  • crTriggerIndex
  • crStickyName
  • crScrollProgress
  • crScrollDirection

But I agree that we should standardise it around triggers and steps, which are the names we've been using elsewhere. So how about instead:

  • crTriggerIndex
  • crStickyName
  • crTriggerProgress
  • crDirection or crTriggerDirection

For crStickyName, it's important to note that it is the name of the sticky element, not the trigger (the trigger generally being the one you'd want to use to drive animation). I actually don't think this one is super necessary, unless you intended for it to be the ID of the trigger instead! If that's the case and we re-wired it, crTriggerId would be good too... or we could remove trigger altogether and just have crIndex, crId, crProgress and crDirection.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, this proposal looks good! I think flagging Trigger vs Sticky is helpful. I know while workign on the js that having those well named helps in thinking through what you're doing.

My plan would be to so a search-and-replace of every incidence of step in our codebase with trigger. Also to change the name of sidebar-col to narrative-col (I guess caption-col is also an option?). The result would be HTML that looks like...

< div class="cr-layout" >
  < div class="narrative-col" >
    < div class="narrative-block" >
      < some block content >
    < /div >
     < div class="narrative-block trigger" possibly-some-data-attributes >
      < some block content >
    < /div >
  < /div >
  < div class="sticky-col" >
    < div class="sticky-stack" >
      < div class="sticky" >
        < some sticky block content >
      < /div >
      < div class="sticky" >
        < some sticky block content >
      < /div >
    < /div >
  < /div >
< /div>

Questions for you:

  1. Are you good on the codebase changes to use trigger and narrative- (I'm open to alternatives but like these fine)
  2. Do you think we should double class .narrative-block (or .caption?) and .trigger on the same div or should the trigger live in a separate div inside the block?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd love to have this added to main, both for the demo and the fixed ojs var code. I'm happy to make these last renaming changes, just let me know!

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Questions for you:

  1. Are you good on the codebase changes to use trigger and narrative- (I'm open to alternatives but like these fine)

Sounds good to me!

  1. Do you think we should double class .narrative-block (or .caption?) and .trigger on the same div or should the trigger live in a separate div inside the block?

Mmm, a double class might be handy. It'd also make the CSS more readable, as we wouldn't have to do > * to select the narrative blocks.

I might need to ask you to make the changes, sorry! I have a bit of cleaning up to do before we hop on the plane 😅

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No prob, I'm on it!

ojsScrollDirection?.define("crScrollDirection",
response.direction);
});

Expand Down
8 changes: 5 additions & 3 deletions demo-block-types.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,9 @@ graph G {

:::{.counter style="position: fixed; top: 10px; right: 10px; background-color: skyblue; border-radius: 5px; padding: 18px 18px 0 18px;"}
```{ojs}
md`Active element: ${crScrollerName}`
md`Element progress: ${crScrollerProgress}`
md`Active trigger: ${crTriggerIndex}`
md`Active element: ${crStickyName}`
md`Element progress: ${(crScrollProgress * 100).toFixed(1)}%`
md`Scroll direction: ${crScrollDirection}`
```
:::
:::
154 changes: 154 additions & 0 deletions docs/gallery/demos/ojs-map/index.qmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
---
title: "OJS map"
format:
closeread-html:
code-fold: true
theme: cosmo
---

Close read makes scrolling progress available to users as [Observable JavasScript](https://quarto.org/docs/interactive/ojs) variables, so you can create Close Read sections with interactive graphics that change as you scroll.

The three variables are:

- `crTriggerIndex`: the index of the active element
- `crScrollerName`: the name of the active element
- `crScrollerProgress`: progress of the active element from 0 to 1
- `crScrollerDirection`: either `"down"` or `"up"`, depending on the direction a user is scrolling

Let's see what we can do with these variables.

I have a list of cities around the world. I'd like to show them off to everyone on a globe, but I'll need to rotate the globe in order to show parts of it.

If we make a globe using [Observable Plot's `geo` mark](https://observablehq.com/@observablehq/plot-projections?collection=@observablehq/plot), we can change its `rotation` option to turn it. That could be linked to the clock if we wanted it to animate on its own, but we can also link it to Close Read's variables to make it spin as we scroll.

Before we start, let's define some cities. Here I've done it in OJS, but you could easily make an R or Python data frame available using `ojs_define()` (or even load a CSV from elsewhere):

```{ojs}
//| label: cities
//| echo: true
//| code-fold: false
cities = [
{ name: "Brisbane", lat: -27.467778, lon: 153.028056 },
{ name: "New Delhi", lat: 28.613889, lon: 77.208889 },
{ name: "Singapore", lat: 1.283333, lon: 103.833333 },
{ name: "Istanbul", lat: 41.013611, lon: 28.955 },
{ name: "Paris", lat: 48.856667, lon: 2.352222 },
{ name: "Nairobi", lat: -1.286389, lon: 36.817222 },
{ name: "São Paulo", lat: -23.55, lon: -46.633333 },
{ name: "Montreal", lat: 45.508889, lon: -73.554167 },
{ name: "Houston", lat: 29.762778, lon: -95.383056 },
{ name: "Vancouver", lat: 49.260833, lon: -123.113889 },
{ name: "Honolulu", lat: 21.306944, lom: -157.858333 }
]
```

Now let's load in some land, so we can distinguish it from ocean:

```{ojs}
//| label: download-land
//| echo: true
world = FileAttachment("naturalearth-land-110m.geojson").json()
```

::::{.cr-layout}

:::{focus-on="map"}
We want our globe to rotate with the scroll progress — between -180 and 180.
:::

:::{focus-on="map"}
Instead of trying to do the maths to scale it ourselves, we can make a scale with d3.
:::

:::{focus-on="map"}
There are six narrative blocks that we want to scale over, but I'd like the scrolling to start a little late and end a little early — by the time the last block has just started.
:::

:::{focus-on="map"}
So between 0.5 (because the scroll starts with the first narrative block of the document) and 5.1. If the numbers go outside this range, we'll _clamp_ them so that the scrolling doesn't continue.
:::

:::{focus-on="map"}
Here's how we create that scale and then use it with Closeread's variables, `crTriggerIndex` and `crScrollProgress`:

```{ojs}
//| label: angle-scale
//| echo: true
//| code-fold: false
angleScale = d3.scaleLinear()
.domain([0.5, 5.1])
.range([-180, 180])
.clamp(true)

angle = angleScale(
jimjam-slam marked this conversation as resolved.
Show resolved Hide resolved
(crTriggerIndex != null ? crTriggerIndex : -1)
+ crScrollProgress)
```
:::

:::{focus-on="map"}
With all that done, we can see our map!
:::

:::{#cr-map}

```{ojs}
//| label: map
Plot.plot({
marks: [
Plot.graticule(),
Plot.geo(world, {
fill: "#222222"
}),
Plot.sphere(),
Plot.dot(cities, {
x: "lon",
y: "lat",
fill: "#eb343d",
stroke: "white",
strokeWidth: 5,
paintOrder: "stroke",
size: 6
}),
Plot.text(cities, {
x: d => d.lon + 2,
y: d => d.lat + 2,
text: "name",
fill: "#eb343d",
stroke: "white",
strokeWidth: 5,
paintOrder: "stroke",
fontSize: 18,
textAnchor: "start"
}),
],
projection: {
type: "orthographic",
rotate: [angle, -10]
}
})
```

:::

::::

:::{.counter style="position: fixed; top: 10px; right: 10px; background-color: skyblue; border-radius: 5px; padding: 18px 18px 0 18px;"}
```{ojs}
md`Active trigger: ${crTriggerIndex}`
md`Active element: ${crStickyName}`
md`Element progress: ${(crScrollProgress * 100).toFixed(1)}%`
md`Scroll direction: ${crScrollDirection}`
md`Angle: ${angle.toFixed(1)}°`
```
:::

And that's all! Let's put some lorem ipsum in so that it can scroll all the way to the end.

:::{style="color: slategrey; font-style: italic;"}
Eu in culpa officia cupidatat nostrud laborum do consequat officia Lorem tempor consectetur pariatur sunt. Veniam culpa dolore laborum nostrud ipsum pariatur ipsum dolore consectetur commodo ex. Non culpa deserunt voluptate. Amet excepteur incididunt deserunt pariatur velit labore do sunt occaecat eiusmod. Tempor proident sint exercitation culpa incididunt sunt proident sunt reprehenderit. Sint ipsum qui id nisi quis officia in. Anim velit minim fugiat qui dolor enim occaecat amet excepteur do aliqua ex adipisicing laboris labore.

Culpa aute sint aliquip in aute enim cillum in exercitation cupidatat ex cupidatat mollit dolore ut. Et culpa minim laborum in ipsum laborum velit laboris fugiat ad culpa cillum. Sit nulla eu minim in nulla. Nulla esse sint occaecat eiusmod in irure in dolor veniam pariatur laboris consectetur sunt laboris excepteur. Dolor dolore ad incididunt consequat. Ad elit ullamco veniam cillum reprehenderit pariatur pariatur nisi ea. Pariatur quis ut deserunt eiusmod ipsum magna ullamco.

Amet velit ea ex. Pariatur anim laboris fugiat labore velit ullamco aute aliquip incididunt. Ut labore voluptate exercitation esse aliquip dolor ex magna occaecat ullamco nisi sit non quis nulla. Elit qui do sunt consectetur officia sint veniam incididunt laboris aute eu nisi qui. Et voluptate consequat sunt commodo dolor dolor veniam minim incididunt culpa anim nulla duis est. Excepteur occaecat tempor veniam magna sit sunt enim sint exercitation dolore fugiat nulla consequat proident. Reprehenderit consequat reprehenderit amet dolore cillum elit dolore officia enim dolor.
:::
134 changes: 134 additions & 0 deletions docs/gallery/demos/ojs-map/naturalearth-land-110m.geojson

Large diffs are not rendered by default.