I'm trying to load an external SVG with
I'm using p5js-svg with p5js and for the most part I don't have any issues, but I'm stuck trying to rotate a loaded SVG. I'm working with both the last versions of pj5s and the library (p5js v1.4.1 and p5js-svg v1.3.3).
Here is the code to reproduce:
function preload() {
svgs = [
loadSVG('/img/plantilla_01.svg'),
loadSVG('/img/plantilla_02.svg'),
loadSVG('/img/plantilla_03.svg')
]}
function setup() {
createCanvas(windowWidth, windowHeight, SVG)
noLoop()
angleMode(DEGREES)
imageMode(CENTER)
}
function draw() {
let posX = 0
let posY = 0
let imagen
for (let index = 0; index < 10; index++) {
let giro = random(-90, 90)
push()
translate(width/2, height/2)
rotate(giro)
imagen = svgs[floor(random(0,3))]
image(imagen, posX, posY, width/10, height/10)
posX += width/10
posY += height/10
pop()
}
}
The loaded SVGs are Adobe Illustrator-produced, and there's no issue with loading and displaying them. Which can be the issue with the rotation?
I think there's a bug with transforms not being applied to image() with the SVG renderer and loadSVG() (and I see you've already raised an issue on github).
One workaround could be using loadImage() instead of loadSVG():
function preload() {
svgs = [
loadImage('/img/plantilla_01.svg'),
loadImage('/img/plantilla_02.svg'),
loadImage('/img/plantilla_03.svg')
]}
function setup() {
createCanvas(windowWidth, windowHeight, SVG)
noLoop()
angleMode(DEGREES)
imageMode(CENTER)
}
function draw() {
let posX = 0
let posY = 0
let imagen
for (let index = 0; index < 10; index++) {
let giro = random(-90, 90)
push()
translate(width/2, height/2)
rotate(giro)
imagen = svgs[floor(random(0,3))]
image(imagen, posX, posY, width/10, height/10)
posX += width/10
posY += height/10
pop()
}
}
I ran into the same issue and used Javascript to fix it. Note that inline CSS only worked in the browser.
function addRotationHack(ele = "rect, circle, ellipse, line, polyline, polygon, path") {
// select shapes in the SVG
var elements = document.querySelectorAll(ele);
// loop through them
for (let i = 0; i < elements.length; i++) {
let deg = Math.random() * 360;
// ❌ Works in the browser, but not Illustrator (doesn't support inline CSS(?))
// elements[i].style.transform = `rotate(${deg}deg)`;
// ✅ instead set the property directly
elements[i].setAttribute("transform", `rotate(${deg})`);
}
}
// ... then call in draw() after your image
image(imagen, posX, posY, width/10, height/10)
addRotationHack();
Related
So I asked a someone to help me with a small project ( ball chases ball you control one)
and he gave me a trigonometric thing, it works very well can someone explain trigonometry to me?
Heres the code he gave me:
var aiX = 0;
var aiY = 0;
var playerX = 200;
var playerY = 200;
draw = function () {
background(255);
playerX = mouseX;
playerY = mouseY;
fill(0, 255, 0);
ellipse(playerX, playerY, 50, 50);
fill(255, 0, 0);
ellipse(aiX, aiY, 50, 50);
var angle = atan2(playerY - aiY, playerX - aiX);
aiX += cos(angle);
aiY += sin(angle);
if (dist(playerX, playerY, aiX, aiY) < 50) {
text("ouch!", 100, 100);
}
};
How i changed it:
var aiX = 200;
var aiY = 0;
function setup() {
playerX = 200;
playerY = 200;
}
draw = function () {
createCanvas(400, 400);
background(255);
let h = hour();
//scare anyone
if (h === 3) {
console.log("I SEE YOU");
let img = createImg(imageData, "YOU CANT HIDE EVEN IF I WONT LOAD");
}
if (isKeyPressed && keyCode === UP_ARROW) {
playerY = playerY - 5;
}
if (isKeyPressed && keyCode === DOWN_ARROW) {
playerY = playerY + 5;
}
if (isKeyPressed && keyCode === RIGHT_ARROW) {
playerX = playerX + 5;
}
if (isKeyPressed && keyCode === LEFT_ARROW) {
playerX = playerX - 5;
}
fill(0, 255, 0);
ellipse(playerX, playerY, 50, 50);
fill(255, 0, 0);
ellipse(aiX, aiY, 50, 50);
function reset() {
playerX = 200;
playerY = 200;
aiX = 200;
aiY = 15;
}
var angle = atan2(playerY - aiY, playerX - aiX);
aiX += cos(angle);
aiY += sin(angle);
if (dist(playerX, playerY, aiX, aiY) < 50) {
console.log("GAMEOVER");
reset();
}
};
const imageData =
"data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD/2wCEAAoHCBISEhIREhIRERISEhISEREREhEPEQ8SGBQZGRgUGBgcIS4lHB4rHxgYJjgmKy8xNTU1GiQ7QDs0Py40NTEBDAwMEA8QGBISGDEdGCExMTQxNDExMTE0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PuTvNOxrLPxUptKce3+SMwwRBt9QWdzGpCM4PMZJNfYnPLvZfvUsKyrN8Sb91N9V+k9RGQEYoMAYAAAeZ2exPwm14cC/p1IOcfDTVHoDKt7aKVWjV601j7orbZ21QtIOdWaXVQTXG/oKCujkzu8m9lCzi48SqVMaQi08PxPPd5d+K1xJxoSlSpYxppKXiZGpVlJtybk3zbeWa6XHW2zvJdXb/q1Hw9IR0jFdjkQWWl3eBuS1s2nx1qcVzlOKA3ssbJU9jwjFPSKk+5lbSeUem31r/oHTxypLReR5fYPmuzZz/kZdHhqerE5VVcM89MnXqIqXFHjicjoqWjJMWceFPGNe5So1XHR80WpTjLHxY0HIXeK1eeiwkn3OVcU1r9ToV4qPXJQq1ea6FMsarnzWCtVmievIozeSuInqo5sbgUvbNsJVZr9OdS0iVTbH2ZKrJPlFc/E3ljRUI8K0wV7C1jTioxR0YovnKWtJoSwef743HHXx+lYNzWqcMW+yPM9sV/eVZy8cD38LCiAAQUTW1eVOcZwbjODUotc00e0bib6/jM0q3DCrBLDzhTXdeJ4iSUasoSUoycZLVOLw0AfUaA8Z3W9o1Wi1Tus1afLi/NHz7nrGytrULqmqlGpGcX0yuJeaGS7gBf8A7oABzqnJeS9Dxj2h/wB0/oKAjY8AAADq7r/3lt/vUADn0Po+4/2Uv2f9p45Y/PU/fL1YAT/I+KeFcqCU+TADhdf8cm4+djY80AG4zUF1yObU/wCooGsp1TuCqwAvn4nTGardb5X9QAtn6lWopFhAB0z4jr6qbS+SXkzzK4+aXm/UAMb+NYRAAEFAAAABvfZX/cMAAPaAABk//9k=";
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/1.4.0/p5.js"></script>
I just started learning p5js about a week ago, so sorry if my code is bad or sloppy...
Editor:https://editor.p5js.org/
Ironically the use of trigonometry in this example is actually unnecessary. The objective is to move a circle positioned at aiX, aiY closer to a second circle positioned at payerX, playerY. It is trivial to compute a vector from the ai position to the player position by taking the difference between the two components in each direction: playerX - aiX is the offset from ai to player in the horizontal direction, and playerY - aiY is the offset from ai to player in the vertical direction. Obviously you won't want to change the position of the ai circle by the entire offset, since that would be instant game over. If you want to fix the speed of movement at a particular level, you simply need to divide each component by the length of the vector, and then if you want a speed other than 1, multiply by the desired speed. Here is the code that does this without any trigonometry:
let aiX = 200;
let aiY = 0;
let playerX = 200;
let playerY = 200;
function setup() {
createCanvas(400, 400);
}
function draw() {
background(255);
if (isKeyPressed && keyCode === UP_ARROW) {
playerY = playerY - 5;
}
if (isKeyPressed && keyCode === DOWN_ARROW) {
playerY = playerY + 5;
}
if (isKeyPressed && keyCode === RIGHT_ARROW) {
playerX = playerX + 5;
}
if (isKeyPressed && keyCode === LEFT_ARROW) {
playerX = playerX - 5;
}
fill(0, 255, 0);
ellipse(playerX, playerY, 50, 50);
fill(255, 0, 0);
ellipse(aiX, aiY, 50, 50);
let offsetX = playerX - aiX;
let offsetY = playerY - aiY;
// pythagorean theorem: given a right triangle, the length of the
// hypotenuse is the square root of side A squared plus side B squared
let length = sqrt(offsetX * offsetX + offsetY * offsetY);
offsetX = offsetX / length;
offsetY = offsetY / length;
// The total distance moved when incrementing will now be 1
aiX += offsetX;
aiY += offsetY;
if (dist(playerX, playerY, aiX, aiY) < 50) {
console.log("GAMEOVER");
reset();
}
}
function reset() {
playerX = 200;
playerY = 200;
aiX = 200;
aiY = 15;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/1.4.0/p5.js"></script>
However, since you asked about the trigonometry, here's how the original example works:
The atan2 function, which implements the inverse tangent function (a.k.a arctangent), takes the vertical offset between two points and the horizontal offset between the two points, and returns the angle of the line between the two points relative to some reference direction (the positive X direction). This is the inverse of the tangent function which takes an angle, and returns the ratio of the opposite to the adjacent sides of a right triangle with that angle. The reason for using atan2 which takes the two offsets separately vs. the atan function which takes the ratio as a single value, is that atan will not return correct angles when the x offset is negative.
Now, once you have the angle of a line from the ai position to the player position, you can use other trigonometric functions to convert back to offsets: namely sine and cosine (i.e. sin and cos). The sine function takes an angle and returns the ratio of the opposite side of a right triangle with that angle and its hypotenuse. The cosine function takes an angle and returns the ratio of the adjacent side of a right triangle with that angle and its hypotenuse. So if you use sin(angle) as the vertical offset to move by, and cos(angle) as the horizontal offset, then you are moving in the direction of angle by an offset of 1.
To better understand all of this I recommend you watch the first few chapters of The Nature of Code by Daniel Shiffman.
I also think this p5.js sketch by spencer eastcott is helpful for visualizing these concepts.
I have been working with html canvas compositing trying to clip a pattern with a mask.
The main issue that I have is that the mask I have comes from an svg with transparencies within the outer most border. I want the entire inside from the outer most border to be filled with the pattern.
Take this SVG for example you can see that there is a single pixel border, then some transparency, and then an opaque red inner blob. The compositing I have done works as the documentation says it should, the single pixel border and the red inner portion pick up the pattern that I want to mask into this shape. The problem is that I want to mask the entire innards starting from the single pixel border.
This is where I think clip might help. But it seems clip only works with manually drawn paths, not paths from an svg (at least that I am aware of).
Is there a way to accomplish what I am trying to do?
Regards,
James
The Path2D constructor accepts an SVG path data argument, that it will parse as the d attribute of an SVG <path> element.
You can then use this Path2D object with the clip() method:
(async () => {
// fetch the svg's path-data
const markup = await fetch("https://upload.wikimedia.org/wikipedia/commons/7/76/Autism_spectrum_infinity_awareness_symbol.svg").then(resp => resp.ok && resp.text());
const doc = new DOMParser().parseFromString(markup, "image/svg+xml");
const pathData = doc.querySelector("[d]").getAttribute("d");
// build our Path2D object and use it
const path = new Path2D(pathData);
const canvas = document.querySelector("canvas");
const ctx = canvas.getContext("2d");
ctx.clip(path);
// draw something that will get clipped
const rad = 30;
for(let y = 0; y < canvas.height; y += rad * 2 ) {
for(let x = 0; x < canvas.width; x += rad * 2 ) {
ctx.moveTo(x+rad, y);
ctx.arc(x, y, rad, 0, Math.PI*2);
}
}
ctx.fillStyle = "red";
ctx.fill();
})().catch(console.error);
<canvas width="792" height="612"></canvas>
If you need to transform this path-data (e.g scale, or rotate), then you can create a second Path2D object, and use its .addPath(path, matrix) method to do so:
// same as above, but smaller
(async () => {
const markup = await fetch("https://upload.wikimedia.org/wikipedia/commons/7/76/Autism_spectrum_infinity_awareness_symbol.svg").then(resp => resp.ok && resp.text());
const doc = new DOMParser().parseFromString(markup, "image/svg+xml");
const pathData = doc.querySelector("[d]").getAttribute("d");
const originalPath = new Path2D(pathData);
const path = new Path2D();
// scale by 0.5
path.addPath(originalPath, { a: 0.5, d: 0.5 });
const canvas = document.querySelector("canvas");
const ctx = canvas.getContext("2d");
ctx.clip(path);
// draw something that will get clipped
const rad = 15;
for(let y = 0; y < canvas.height; y += rad * 2 ) {
for(let x = 0; x < canvas.width; x += rad * 2 ) {
ctx.moveTo(x+rad, y);
ctx.arc(x, y, rad, 0, Math.PI*2);
}
}
ctx.fillStyle = "red";
ctx.fill();
})().catch(console.error);
<canvas width="396" height="306"></canvas>
Hi guys I been leanring WebGL and trying to make a Tetris game out of it.
I have a couple of questions I'd like to ask:
For this game I wanted to first draw the grid as the background. However I noticed that after I drew the line, if I use gl.clear(gl.COLOR_BUFFER_BIT ); after, it will clear all the lines I drew before. I understand that gl.clear(gl.COLOR_BUFFER_BIT ); is about clearing the color buffer (and you probably will ask why I would want to do that. Just bear with me. ). Then I tried use gl.uniform4f( uColor, 0, 0, 0, 1); to send the color again to the fragment shader but it doesn't help.
The snippet is like this
window.onload = function(){
getGLContext();
initShaders();
drawLines( 0, 0, 400,400 );
gl.clear(gl.COLOR_BUFFER_BIT );
gl.uniform4f( uColor, 0, 0, 0, 1);
}
For the game I need the grid as background and I need requestAnimationFrame for the game loop and will render Tetrominos inside the loop. Therefore after drawing the line I used this draw() to draw other Tetrominos. However it removes the line I drew before. And when I comment out gl.clear(gl.COLOR_BUFFER_BIT ); inside draw(), it will remove the line along with background color.
function draw() {
gl.clear(gl.COLOR_BUFFER_BIT );
gl.drawArrays(gl.TRIANGLES, 0, index*6);
requestAnimationFrame(draw);
}
Here is the demo: https://codepen.io/zhenghaohe/pen/LqxpjB
Hope you could answer these two questions. Thanks!
This is generally the way WebGL works.
WebGL is just draws into a rectangle of pixels. There is no memory of primitives. There is no structure. There is just code and the resulting canvas which is an rectangle of pixels.
Most WebGL programs/pages clear the entire canvas every frame and redraw 100% of the things they want to show every time they draw. For tetris the general code might be something like
function render() {
clear the canvas
draw the grid
draw all the stable pieces
draw the current piece
draw the next piece
draw the effects
draw the score
}
Any knowledge of primitives or other structure is entirely up to your code.
If you want the grid lines to be static then either set a static background with CSS or use another canvas
Using a background:
const gl = document.querySelector('#c').getContext('webgl');
function render(time) {
time *= 0.001;
gl.clearColor(0, 0, 0, 0);
gl.clear(gl.COLOR_BUFFER_BIT);
drawBlocks(gl, time);
requestAnimationFrame(render);
}
requestAnimationFrame(render);
// --- below this line not important to the answer
function drawBlocks(gl, time) {
gl.enable(gl.SCISSOR_TEST);
const numBlocks = 5;
for (let i = 0; i < numBlocks; ++i) {
const u = i / numBlocks;
gl.clearColor(i / 5, i / 2 % 1, i / 3 % 1, 1);
const x = 150 + Math.sin(time + u * Math.PI * 2) * 130;
const y = 75 + Math.cos(time + u * Math.PI * 2) * 55;
gl.scissor(x, y, 20, 20);
gl.clear(gl.COLOR_BUFFER_BIT);
}
gl.disable(gl.SCISSOR_TEST);
}
#c {
background-image: url(https://i.imgur.com/ZCfccZh.png);
}
<canvas id="c"></canvas>
Using 2 canvases
// this is the context for the back canvas. It could also be webgl
// using a 2D context just to make the sample simpler
const ctx = document.querySelector('#back').getContext('2d');
drawGrid(ctx);
// this is the context for the front canvas
const gl = document.querySelector('#front').getContext('webgl');
function render(time) {
time *= 0.001;
gl.clearColor(0, 0, 0, 0);
gl.clear(gl.COLOR_BUFFER_BIT);
drawBlocks(gl, time);
requestAnimationFrame(render);
}
requestAnimationFrame(render);
// --- below this line not important to the answer
function drawBlocks(gl, time) {
gl.enable(gl.SCISSOR_TEST);
const numBlocks = 5;
for (let i = 0; i < numBlocks; ++i) {
const u = i / numBlocks;
gl.clearColor(i / 5, i / 2 % 1, i / 3 % 1, 1);
const x = 150 + Math.sin(time + u * Math.PI * 2) * 130;
const y = 75 + Math.cos(time + u * Math.PI * 2) * 55;
gl.scissor(x, y, 20, 20);
gl.clear(gl.COLOR_BUFFER_BIT);
}
gl.disable(gl.SCISSOR_TEST);
}
function drawGrid(ctx) {
// draw grid
ctx.translate(-10.5, -5.5);
ctx.beginPath();
for (let i = 0; i < 330; i += 20) {
ctx.moveTo(0, i);
ctx.lineTo(330, i);
ctx.moveTo(i, 0);
ctx.lineTo(i, 300);
}
ctx.strokeStyle = "blue";
ctx.stroke();
}
#container {
position: relative; /* required so we can position child elements */
}
#front {
position: absolute;
left: 0;
top: 0;
}
<div id="container">
<canvas id="back"></canvas>
<canvas id="front"></canvas>
</div>
As for why it clears even if you didn't call clear that's because that's whqt the spec says it's supposed to do
See: Why WebGL 'clear' draw to front buffer?
In a word game I am trying to draw score as white numbers above a blue (or red) rectangle:
For example, in the above screenshot it is the number "13".
Here is my entire class Score.js (with currently hardcoded WIDTH and HEIGHT):
"use strict";
function Score(color) {
PIXI.Container.call(this);
this.interactive = false;
this.buttonMode = false;
this.visible = false;
this.bgGraphics = new PIXI.Graphics();
this.bgGraphics.beginFill(color, 1);
this.bgGraphics.drawRect(0, 0, Score.WIDTH, Score.HEIGHT);
this.bgGraphics.endFill();
this.addChild(this.bgGraphics);
this.text = new PIXI.Text('XXX', {font: '20px Arial', fill: 0xFFFFFF});
this.text.x = 1;
this.text.y = 1;
this.addChild(this.text);
}
Score.prototype = Object.create(PIXI.Container.prototype);
Score.prototype.constructor = Score;
Score.WIDTH = 36;
Score.HEIGHT = 24;
Score.prototype.setText = function(str) {
this.text.text = str;
}
I wonder, how to modify my setText() function, so that a new rectangle is drawn on each call - as a bounding rectangle for the str argument?
I have looked at the PIXI.Text.getBounds() method, but it returns a Matrix and not a Rectangle...
I think you can just use this.text.width. This has historically had some bugs associated with it, but it should be working right in the latest version.
In Phaser (2.4.x), I'm drawing a circle around a sprite when it is dragged:
function dragStart(sprite, pointer, dragX, dragY) {
var graphics = game.add.graphics(0, 0);
graphics.lineStyle(6, 0x909090, 0.3);
graphics.drawCircle(dragX, dragY, 200);
}
That works fine, but now I need to remove the circle when the drag ends, and I can't figure that part out:
function dragStop() {
// ?
}
Is it possible to remove graphics? Is there a better or simpler option to draw a circle and remove it later?
You could kill() the object
But be carefull with the scope of the var you want kill (you are defining it inside the function).
Or you could just create the graphic and then show or hide depending of your event (drag)
I leave you a very simple example with both solutions:
var game = new Phaser.Game(500, 500, Phaser.AUTO, 'game');
var mainState = {
create:function(){
var graphics = game.add.graphics(0, 0);
graphics.lineStyle(6, 0x909090, 0.3);
graphics.drawCircle(game.world.centerX+100,game.world.centerY+100, 200);
console.log(graphics);
setTimeout(function(){
graphics.kill();
},2000);
this.graphics2 = game.add.graphics(0, 0);
this.graphics2.lineStyle(6, 0xff0000, 1);
this.graphics2.drawCircle(game.world.centerX-100,game.world.centerY-100, 200);
this.graphics2.visible = false;
this.show_later = game.time.now + 2000;
this.hide_again = game.time.now + 4000;
},
update:function(){
if(this.show_later < game.time.now){
this.graphics2.visible = false;
}
if(this.hide_again < game.time.now){
this.graphics2.visible = true;
}
},
};
game.state.add('main', mainState);
game.state.start('main');
<script src="https://cdnjs.cloudflare.com/ajax/libs/phaser/2.4.4/phaser.min.js"></script>
<div id="game"></div>