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

Replace alert in canvas_api_tutorial #34629

Merged
merged 4 commits into from
Jul 5, 2024

Conversation

evelinabe
Copy link
Contributor

@evelinabe evelinabe commented Jul 4, 2024

Description

✍️ Replace alert in Web/API/Canvas_API/Tutorial/Applying_styles_and_colors

Motivation

❓ Current example is not working, there's an ongoing issue to replace all alerts. In this case I resolved it by removing the else statement with the alert and placing a minimum value on the form input, which is the standard way of handling minimum values.

Additional details

🔗 #7566

Related issues and pull requests

🔨 Fixes (Partly) #7566

@evelinabe evelinabe requested a review from a team as a code owner July 4, 2024 17:32
@evelinabe evelinabe requested review from wbamberg and removed request for a team July 4, 2024 17:32
@github-actions github-actions bot added the Content:WebAPI Web API docs label Jul 4, 2024
@github-actions github-actions bot added the size/xs [PR only] 0-5 LoC changed label Jul 4, 2024
Copy link
Contributor

github-actions bot commented Jul 4, 2024

Preview URLs

(comment last updated: 2024-07-05 17:19:22)

@Josh-Cena
Copy link
Member

Please don't use the substring "fixes #xxx" in your description unless you are actually closing that issue, otherwise GH automatically links it.

@wbamberg
Copy link
Collaborator

wbamberg commented Jul 4, 2024

Thanks for your PR! Unfortunately the example still doesn't work. I wonder if this is actually because of the way it's triggering the redraw call:

.

If I replace it with a more conventional click handler it seems to work fine. Also I think we can get rid of this regex:

if (document.getElementById("miterLimit").value.match(/\d+(\.\d+)?/)) {

...and call checkValidity() instead:


```js
function draw() {
  const ctx = document.getElementById("canvas").getContext("2d");

  // Clear canvas
  ctx.clearRect(0, 0, 150, 150);

  // Draw guides
  ctx.strokeStyle = "#09f";
  ctx.lineWidth = 2;
  ctx.strokeRect(-5, 50, 160, 50);

  // Set line styles
  ctx.strokeStyle = "#000";
  ctx.lineWidth = 10;

  // check input
  if (document.getElementById("miterLimit").checkValidity()) {
    ctx.miterLimit = parseFloat(document.getElementById("miterLimit").value);
  }

  // Draw lines
  ctx.beginPath();
  ctx.moveTo(0, 100);
  for (let i = 0; i < 24; i++) {
    const dy = i % 2 === 0 ? 25 : -25;
    ctx.lineTo(Math.pow(i, 1.5) * 2, 75 + dy);
  }
  ctx.stroke();
  return false;
}
```

```html hidden
<table>
  <tr>
    <td>
      <canvas id="canvas" width="150" height="150" role="presentation"></canvas>
    </td>
    <td>
      Change the <code>miterLimit</code> by entering a new value below and
      clicking the redraw button.<br /><br />
      <label for="miterLimit">Miter limit</label>
      <input type="number" size="3" id="miterLimit" />
      <input type="button" id="redraw" value="Redraw" />
    </td>
  </tr>
</table>
```

```js hidden
document.getElementById("miterLimit").value = document
  .getElementById("canvas")
  .getContext("2d").miterLimit;
draw();

const redraw = document.querySelector("#redraw");
redraw.addEventListener("click", draw);
```

{{EmbedLiveSample("A_demo_of_the_miterLimit_property", "", "180")}}

@evelinabe evelinabe requested review from mdn-bot and a team as code owners July 5, 2024 17:11
@evelinabe evelinabe requested review from teoli2003 and estelle and removed request for a team July 5, 2024 17:11
@github-actions github-actions bot added Content:CSS Cascading Style Sheets docs Content:HTML Hypertext Markup Language docs Content:JS JavaScript docs Content:Other Any docs not covered by another "Content:" label Content:HTTP HTTP docs Content:Accessibility Accessibility docs Content:SVG SVG docs Content:Media Media docs labels Jul 5, 2024
@github-actions github-actions bot added size/l [PR only] 501-1000 LoC changed and removed size/xs [PR only] 0-5 LoC changed labels Jul 5, 2024
@evelinabe evelinabe force-pushed the 7566-replace-alert-tutorial branch from bfa407f to 9355f54 Compare July 5, 2024 17:17
@github-actions github-actions bot added size/s [PR only] 6-50 LoC changed and removed Content:CSS Cascading Style Sheets docs Content:HTML Hypertext Markup Language docs Content:JS JavaScript docs Content:Other Any docs not covered by another "Content:" label Content:HTTP HTTP docs Content:Accessibility Accessibility docs Content:SVG SVG docs Content:Media Media docs Content:Learn:Django Learning area Django docs Content:wasm WebAssembly docs Content:Glossary Glossary entries Content:WebDriver WebDriver docs Content:Security Security docs Content:Firefox Content in the Mozilla/Firefox subtree Content:Meta Content in the meta docs Content:Learn:HTML Learning area HTML docs Content:Manifest Content:PWA Progressive Web Apps content system [PR only] Infrastructure and configuration for the project size/l [PR only] 501-1000 LoC changed labels Jul 5, 2024
@evelinabe
Copy link
Contributor Author

Thanks for your PR! Unfortunately the example still doesn't work. I wonder if this is actually because of the way it's triggering the redraw call:


.

If I replace it with a more conventional click handler it seems to work fine. Also I think we can get rid of this regex:

if (document.getElementById("miterLimit").value.match(/\d+(\.\d+)?/)) {

...and call checkValidity() instead:


```js
function draw() {
  const ctx = document.getElementById("canvas").getContext("2d");

  // Clear canvas
  ctx.clearRect(0, 0, 150, 150);

  // Draw guides
  ctx.strokeStyle = "#09f";
  ctx.lineWidth = 2;
  ctx.strokeRect(-5, 50, 160, 50);

  // Set line styles
  ctx.strokeStyle = "#000";
  ctx.lineWidth = 10;

  // check input
  if (document.getElementById("miterLimit").checkValidity()) {
    ctx.miterLimit = parseFloat(document.getElementById("miterLimit").value);
  }

  // Draw lines
  ctx.beginPath();
  ctx.moveTo(0, 100);
  for (let i = 0; i < 24; i++) {
    const dy = i % 2 === 0 ? 25 : -25;
    ctx.lineTo(Math.pow(i, 1.5) * 2, 75 + dy);
  }
  ctx.stroke();
  return false;
}
<table>
  <tr>
    <td>
      <canvas id="canvas" width="150" height="150" role="presentation"></canvas>
    </td>
    <td>
      Change the <code>miterLimit</code> by entering a new value below and
      clicking the redraw button.<br /><br />
      <label for="miterLimit">Miter limit</label>
      <input type="number" size="3" id="miterLimit" />
      <input type="button" id="redraw" value="Redraw" />
    </td>
  </tr>
</table>
document.getElementById("miterLimit").value = document
  .getElementById("canvas")
  .getContext("2d").miterLimit;
draw();

const redraw = document.querySelector("#redraw");
redraw.addEventListener("click", draw);

{{EmbedLiveSample("A_demo_of_the_miterLimit_property", "", "180")}}

I was only focusing on the replacement for the alert functionality, but you're right, the example is not working as is. I have updated the PR with your proposed changes, thanks!

Copy link
Collaborator

@wbamberg wbamberg left a comment

Choose a reason for hiding this comment

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

👍 thank you @evelinabe !

@wbamberg wbamberg merged commit 36d589b into mdn:main Jul 5, 2024
8 checks passed
@Josh-Cena Josh-Cena mentioned this pull request Jul 5, 2024
26 tasks
@evelinabe evelinabe deleted the 7566-replace-alert-tutorial branch July 5, 2024 19:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Content:WebAPI Web API docs size/s [PR only] 6-50 LoC changed
3 participants