P5.js not working in blogger

Hi,
Does p5.js work on blogger.com platform? It is not working for me. Tried with simple p5.js with canvas and included the cdn link but does not work. Has anyone tried p5.js on blogger?

1 Like

Hi @processing_user,

What do you mean by “It is not working for me” ? Do you have any error messages in the web console? Is the script really executed?

I think the easiest way is to embed the sketch to your page as an iframe. For example, if you are on p5.js web editor, select share and there is an HTML code for embed that you can directly paste into your blog page (HTML view)

thanks, iframe works but still how can i do it without iframe?

in blogger platform we just paste the html code and javascript in page or blog. with other js with html/css it works perfectly but with p5.js the same thing does not work

All right can you tell us what is not working?
Can you verify if the CDN link is working?
Are the script tags correctly ordered? (See this)
Are there any errors in your JS code? (Press F12 to open the developer console and paste any errors here)

1 Like

hi, the following is the code on single html page:

<html>
<head>
  <style>   
  canvas {
    outline: black 3px solid;
  border-radius: 12px;
}
    </style>
    <script src="https://cdn.jsdelivr.net/npm/p5@1.4.0/lib/p5.js"></script>
</head>

<body>
  <script>
  function setup() {
createCanvas(480, 120);
}


function draw() {
if (mouseIsPressed) {
fill(0);
} else {
fill(155);
}
ellipse(mouseX, mouseY, 60, 60);
}
  </script>
</body>
</html>

the cdn link is working.
below is picture of blog page in blogger.com(free blog platform):

Thank you for that, it looks fine.

Can you do the following when you are on your blog’s page?

hi, no errors can be seen:

the blog shows only rectangle:

this worked for me

<script src="https://cdn.jsdelivr.net/npm/p5@1.4.0/lib/p5.js"></script>

<div id="p5canvas"></div>

<script>
  function setup() {
createCanvas(480, 120).parent("p5canvas");
}


function draw() {
if (mouseIsPressed) {
fill(0);
} else {
fill(155);
}
ellipse(mouseX, mouseY, 60, 60);
}
</script>

the problem is the editor on blogger is not the entire html page, but a portion of the page that renders the content. So you cannot add tags like <html> and <body>. The other issue is that if you don’t select parent() of the canvas, it will appear at the bottom of the page. You need to place <div> where you want to show your canvas, and attach the canvas to that div.

3 Likes

awsome, :+1: tested and good to go, many thanks

1 Like

you’re welcome! I don’t earn any points by this but if you could set the post above as “solution” it might help people visiting here in the future :slight_smile:

2 Likes