Skip to content

Commit

Permalink
scale lesson 1 ok
Browse files Browse the repository at this point in the history
  • Loading branch information
holtzy committed Oct 24, 2024
1 parent 01cc854 commit cf01cca
Show file tree
Hide file tree
Showing 5 changed files with 106 additions and 56 deletions.
45 changes: 35 additions & 10 deletions component/interactiveTeaching/CircleScaleExercise.tsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
import { Button } from '@/component/UI/button';
import { useCallback, useState } from 'react';

const INITIAL_POSITIONS = [
{ id: 1, cx: 370, value: 0 },
{ id: 2, cx: 280, value: 50 },
{ id: 3, cx: 150, value: 60 },
{ id: 4, cx: 260, value: 82 },
{ id: 5, cx: 80, value: 100 },
];

export const CircleScaleExercise = () => {
// Initial positions of the circles
const [circles, setCircles] = useState([
{ id: 1, cx: 200, value: 0 },
{ id: 2, cx: 220, value: 50 },
{ id: 3, cx: 240, value: 60 },
{ id: 4, cx: 260, value: 82 },
{ id: 5, cx: 280, value: 100 },
]);
const [circles, setCircles] = useState(INITIAL_POSITIONS);
const [draggingCircleId, setDraggingCircleId] = useState(null);

// Handle mouse down event to start dragging
Expand Down Expand Up @@ -47,7 +49,7 @@ export const CircleScaleExercise = () => {
<div className="mx-auto">
<svg
width={500}
height={400}
height={320}
overflow={'visible'}
onMouseMove={handleMouseMove}
onMouseUp={handleMouseUp}
Expand All @@ -65,15 +67,30 @@ export const CircleScaleExercise = () => {
fillOpacity={1}
onMouseDown={(e) => handleMouseDown(e, circle.id)}
cursor={'pointer'}
style={{
transition: 'cx 0.5s ease', // Smooth transition for the x-position
}}
onMouseEnter={(e) => (e.target.style.transition = 'none')} // Disable transition on hover
onMouseLeave={(e) =>
(e.target.style.transition = 'transform 0.5s ease')
} // Re-enable transition when hover ends
/>
<text
x={circle.cx}
x={0}
y={140}
textAnchor="middle"
alignmentBaseline="central"
fontSize={12}
cursor={'pointer'}
pointerEvents={'none'}
style={{
transition: 'transform 0.5s ease', // Smooth transition for position
transform: `translateX(${circle.cx}px)`,
}}
onMouseEnter={(e) => (e.target.style.transition = 'none')} // Disable transition on hover
onMouseLeave={(e) =>
(e.target.style.transition = 'transform 0.5s ease')
} // Re-enable transition when hover ends
>
{circle.value}
</text>
Expand Down Expand Up @@ -118,7 +135,15 @@ export const CircleScaleExercise = () => {
</svg>
</div>

<div>
<div className="w-full flex justify-center gap-2">
<Button
variant={'outline'}
onClick={() => {
setCircles(INITIAL_POSITIONS);
}}
>
Reset
</Button>
<Button
onClick={() => {
setCircles([
Expand Down
32 changes: 32 additions & 0 deletions pages/course/scales/introduction.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { lessonList } from '@/util/lessonList';
import { CodeBlock } from '@/component/UI/CodeBlock';
import { Button } from '@/component/UI/button';
import { CircleScaleExercise } from '@/component/interactiveTeaching/CircleScaleExercise';
import { ExerciseAccordion } from '@/component/ExerciseAccordion';

const previousURL = '/course/svg/path-element';
const currentURL = '/course/scales/introduction';
Expand All @@ -14,6 +15,10 @@ const seoDescription = '';
export default function Home() {
const currentLesson = lessonList.find((l) => l.link === currentURL);

if (!currentLesson) {
return null;
}

return (
<LayoutCourse
title={currentLesson.name}
Expand Down Expand Up @@ -67,6 +72,33 @@ export default function Home() {

<CircleScaleExercise />

<p>
<br /> <br />
</p>

<ExerciseAccordion
localStorageId={currentLesson.link}
exercises={[
{
title: <span>Let's do some math</span>,
content: (
<>
<p>
I'm pretty sure you managed to put the circles at the right
position.
</p>
<p>
But, before reading the following, try to write down the{' '}
<b>function</b> that allows to go from a value in the dataset
to a position in pixel.
</p>
<p>The answer is coming below!</p>
</>
),
},
]}
/>

<h2>How it actually works</h2>

<h3>The obvious part:</h3>
Expand Down
34 changes: 0 additions & 34 deletions pages/course/svg/d3-shape.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -441,40 +441,6 @@ const exercices: Exercise[] = [
solutionSandbox: 'exercise/d3AreaFunctionSolution',
},

{
whyItMatters: (
<>
<p>
Using the <code>area()</code> function is almost the same as the{' '}
<code>line()</code> function.
</p>
<p>
That's the magic of the <code>d3-shape</code> module, you switch from
one shape to the other easily!
</p>
</>
),
toDo: (
<ul>
<li>
Let's switch to an area chart, thanks to the <code>d3.area()</code>{' '}
function. Three methods must be chained to <code>area()</code>:
</li>
<li>
<code>x()</code> is the same as for the line chart.
</li>
<li>
<code>y0()</code>: Y coordinate of the bottom of the area
</li>
<li>
<code>y1()</code> Y coordinate of the top.
</li>
</ul>
),
practiceSandbox: 'exercise/d3LineMultipleLineChartPractice',
solutionSandbox: 'exercise/d3LineMultipleLineChartSolution',
},

{
whyItMatters: (
<>
Expand Down
49 changes: 38 additions & 11 deletions pages/course/svg/tips-and-tricks.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
} from '@/component/UI/select';
import { Sidenote } from '@/component/SideNote';
import { MoveHorizontal, MoveVertical } from 'lucide-react';
import { CodeBlock } from '@/component/UI/CodeBlock';

const previousURL = '/course/svg/path-element';
const currentURL = '/course/svg/tips-and-tricks';
Expand Down Expand Up @@ -194,13 +195,35 @@ export default function Home() {
This is especially useful for applying transformations, styles, or
events to a collection of elements as a single unit.
</p>

<CodeBlock
code={`
<svg width="200" height="200" xmlns="http://www.w3.org/2000/svg">
<g fill="none" stroke="black">
<circle cx="50" cy="50" r="40" />
<rect x="100" y="100" width="80" height="80" />
<line x1="20" y1="20" x2="180" y2="180" />
</g>
</svg>
`.trim()}
/>
{/* -
-
-
-
-
-
-
-
-
*/}
<h2>4️⃣ Stroke, Fill, and Color: Different from HTML</h2>
<p>
In SVG, the concepts of <code>stroke</code>, <code>fill</code>, and{' '}
<code>color</code> work differently than in standard HTML. The{' '}
<code>fill</code> property controls the interior color of shapes, while{' '}
<code>stroke</code> affects the outline. Unlike <code>div</code>{' '}
<code>color</code> work differently than in standard HTML.
</p>
<p>
The <code>fill</code> property controls the interior color of shapes,
while <code>stroke</code> affects the outline. Unlike <code>div</code>{' '}
elements, SVG shapes don’t have separate properties for borders and
backgrounds; instead, you use <code>stroke</code> and <code>fill</code>{' '}
to control these aspects.
Expand All @@ -221,16 +244,20 @@ export default function Home() {
specificity rules.
</p>

<h2>6️⃣ Text Specificity in SVG vs. HTML</h2>
<h2>6️⃣ Text Wrapping</h2>
<p>
In SVG, there is{' '}
<b>no built-in functionality for automatic text wrapping</b> like you
would find in HTML or CSS. 😱
</p>
<p>
In SVG, there is no built-in functionality for automatic text wrapping
like you would find in HTML or CSS. You have to manage text wrapping
manually or use JavaScript libraries to handle it. You need to calculate
where to break the text and create multiple
You have to manage text wrapping manually or use JavaScript libraries to
handle it.
</p>
<p>
This is very super annoying. We will talk about workarounds in this
course.
This is very very annoying. In practice, we'll often use a HTML layer on
top of the SVG layer to add text (like in tooltips) to make our life
simpler.
</p>

<h2>7️⃣ SVG Dimensions: The Impact of “100%”</h2>
Expand Down
2 changes: 1 addition & 1 deletion util/lessonList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ export const lessonList: Lesson[] = [
),
readTime: 4,
link: '/course/svg/d3-shape',
status: 'wip',
status: 'free',
moduleId: 'svg',
},
{
Expand Down

0 comments on commit cf01cca

Please sign in to comment.