how to make curve line using css with animate? - css

I am trying to create curve line like this using css:

You can try canvas:
var c = document.getElementById("myCanvas");
var ctx = c.getContext("2d");
ctx.strokeStyle = '#ff0000';
ctx.beginPath();
ctx.moveTo(20, 20);
ctx.lineTo(100, 20);
ctx.bezierCurveTo(105, 30, 110, 30, 115, 20);
ctx.lineTo(150, 20);
ctx.stroke();
Example code: https://www.w3schools.com/code/tryit.asp?filename=FW7CGRNXNTWQ

Related

PixiJS: Affine projection vs bilinear one

Let's say there is an arbitrary composite shape in PixiJS:
I need to project a texture onto it by some kind of a grid warp in a way, so the result would be:
I have tried to do it via PIXI.SimplePlane and it seems that default projection algorithm at this framework is affine transformation. And the result have to be based on quad bi-linear projection.
Yes, PixiJS has additional projections plugin (https://pixijs.io/examples/#/plugin-projection/quad-homo.js), but it seems that output could be controlled just by four corner points. In my case, I need more. At least 10 or even 15.
If I couldn't use projection plugin, maybe there is a way to implement a custom shader?!
let pattern64 = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAgAAAAEACAIAAABK8lkwAAAACXBIWXMAAAAnAAAAJwEqCZFPAAAFIGlUWHRYTUw6Y29tLmFkb2JlLnhtcAAAAAAAPD94cGFja2V0IGJlZ2luPSLvu78iIGlkPSJXNU0wTXBDZWhpSHpyZVN6TlRjemtjOWQiPz4gPHg6eG1wbWV0YSB4bWxuczp4PSJhZG9iZTpuczptZXRhLyIgeDp4bXB0az0iQWRvYmUgWE1QIENvcmUgNS42LWMxNDAgNzkuMTYwNDUxLCAyMDE3LzA1LzA2LTAxOjA4OjIxICAgICAgICAiPiA8cmRmOlJERiB4bWxuczpyZGY9Imh0dHA6Ly93d3cudzMub3JnLzE5OTkvMDIvMjItcmRmLXN5bnRheC1ucyMiPiA8cmRmOkRlc2NyaXB0aW9uIHJkZjphYm91dD0iIiB4bWxuczp4bXA9Imh0dHA6Ly9ucy5hZG9iZS5jb20veGFwLzEuMC8iIHhtbG5zOmRjPSJodHRwOi8vcHVybC5vcmcvZGMvZWxlbWV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AKAEAiBIAgCgBAIgSAIAoAQCIEgCAKAEAiBIAgCgBAIgSAIAoAQCIEgCAKAEAiBIAgCgBAIgSAIAoAQCIEgCAKAEAiBIAgCgBAIgSAIAoAQCIEgCAKAEAiBIAgCgBAIgSAIAoAQCIEgCAKAEAiBIAgCgBAIha7/d7egOMeZ5negKM8QMAiBIAgCgBAIgSAIAoAQCIEgCAKAEAiBIAgCgBAIgSAIAoAQCIEgCAKAEAiBIAgCgBAIgSAIAoAQCIEgCAKAEAiBIAgCgBAIgSAIAoAQCIEgCAKAEAiBIAgCgBAIgSAIAoAQCIEgCAKAEAiBIAgCgBAIgSAIAoAQCIEgCAKAEAiBIAgCgBAIgSAIAoAQCIEgCAKAEAiBIAgCgBAIgSAIAoAQCIEgCAKAEAiBIAgCgBAIgSAIAoAQCIEgCAKAEAiBIAgCgBAIgSAIAoAQCIEgCAKAEAiBIAgCgBAIj6By1EGKDptaHUAAAAAElFTkSuQmCC";
const app = new PIXI.Application({ width: 512, height: 324, antialias: true });
document.body.appendChild(app.view);
app.loader.add('pattern', pattern64).load(inits);
function inits() {
const graphics = new PIXI.Graphics();
let path = [34, 100, 176, 100, 176, 224, 34, 224];
graphics.lineStyle(0);
graphics.beginFill(0xBCBCBC, 1);
graphics.drawPolygon(path);
graphics.endFill();
path = [176, 100, 258, 60, 258, 264, 176, 224];
graphics.lineStyle(0);
graphics.beginFill(0xCDCDCD, 1);
graphics.drawPolygon(path);
graphics.endFill();
path = [258, 60, 422, 104, 422, 220, 256, 264];
graphics.lineStyle(0);
graphics.beginFill(0x606060, 1);
graphics.drawPolygon(path);
graphics.endFill();
path = [422, 104, 478, 104, 478, 220, 422, 220];
graphics.lineStyle(0);
graphics.beginFill(0x909090, 1);
graphics.drawPolygon(path);
graphics.endFill();
app.stage.addChild(graphics);
const texture = app.loader.resources.pattern.texture;
const plane = new PIXI.SimplePlane(texture, 5, 3);
app.stage.addChild(plane);
let buffer = plane.geometry.getBuffer('aVertexPosition');
// graphics.beginFill(0xFF0000);
// graphics.drawRect(buffer.data[20], buffer.data[21], 8, 8);
// graphics.endFill();
//manually set just for this demo
buffer.data[0] = 34;
buffer.data[1] = 100;
buffer.data[2] = 176;
buffer.data[3] = 100;
buffer.data[4] = 258;
buffer.data[5] = 60;
buffer.data[6] = 422;
buffer.data[7] = 104;
buffer.data[8] = 478;
buffer.data[9] = 104;
buffer.data[10] = 34;
buffer.data[11] = 162;
buffer.data[12] = 176;
buffer.data[13] = 162;
buffer.data[14] = 258;
buffer.data[15] = 162;
buffer.data[16] = 422;
buffer.data[17] = 162;
buffer.data[18] = 478;
buffer.data[19] = 162;
buffer.data[20] = 34;
buffer.data[21] = 224;
buffer.data[22] = 176;
buffer.data[23] = 224;
buffer.data[24] = 258;
buffer.data[25] = 264;
buffer.data[26] = 422;
buffer.data[27] = 220;
buffer.data[28] = 478;
buffer.data[29] = 220;
buffer.update();
}
body { margin: 0; }
<script src="https://cdnjs.cloudflare.com/ajax/libs/pixi.js/6.5.5/browser/pixi.js"></script>

How to make text to be always aligned even on curved path

I‘m building a "flow-builder" and I need to get this result:
Currently I have this as my edge :
As you can see the text on the edge is matching the edge itself and I need it to align differently like in the first picture
I am new to path/textPath so i cant figure this out.
Here is my "Edge" component
import React from 'react';
import { getSmoothStepPath, getMarkerEnd } from 'react-flow-renderer';
export default function DefaultEdge({
id,
sourceX,
sourceY,
targetX,
targetY,
sourcePosition,
targetPosition,
style = {},
data,
arrowHeadType,
markerEndId,
}) {
const edgePath = getSmoothStepPath({ sourceX, sourceY, sourcePosition, targetX, targetY, targetPosition });
const markerEnd = getMarkerEnd(arrowHeadType, markerEndId);
return (
<>
<path id={id} style={style} className="react-flow__edge-path" d={edgePath} markerEnd={markerEnd} />
<text>
<textPath href={`#${id}`} style={{ fontSize: '12px' }} startOffset="90%" textAnchor="middle" >
{data?.text || 'default text'}
</textPath>
</text>
</>
);
}
Since you tagged canvas I'll assume this is being done on a canvas. I can't help with the code you provided but can give an example of how to use the methods built in to canvas to achieve what you want.
let canvas = document.getElementById("canvas");
let ctx = canvas.getContext("2d");
canvas.width = 300;
canvas.height = 300;
let pt1 = {x: 50, y: 50};
let pt2 = {x: 200, y: 50};
let pt3 = {x: 230, y: 150};
let pt4 = {x: 230, y: 220};
let pt5 = {x: 230, y: 250};
let nodes = [];
class Nodes {
constructor(x, y) {
this.x = x;
this.y = y;
this.r = 5;
}
draw() {
ctx.beginPath();
ctx.arc(this.x, this.y, 5, 0, Math.PI*2);
ctx.fill();
}
}
let node1 = nodes.push(new Nodes(pt1.x, pt1.y));
let node2 = nodes.push(new Nodes(pt3.x, pt3.y));
let node3 = nodes.push(new Nodes(pt4.x, pt4.y));
let node4 = nodes.push(new Nodes(pt5.x, pt5.y));
for (let i=0;i<nodes.length;i++) {
nodes[i].draw();
}
class Edge {
constructor(x, y, lw) {
this.x = x;
this.y = y;
this.lw = lw;
}
draw() {
ctx.strokeStyle = 'black';
ctx.beginPath();
ctx.moveTo(pt1.x, pt1.y);
ctx.lineTo(pt2.x, pt2.y);
ctx.bezierCurveTo(230, 50, 230, 50, 230, 80);
ctx.lineTo(pt3.x, pt3.y);
ctx.moveTo(pt4.x, pt4.y);
ctx.lineTo(pt5.x, pt5.y);
ctx.stroke();
}
}
let edge1 = new Edge();
edge1.draw()
ctx.font = 'bold 20px arial';
ctx.textAlign = 'center';
ctx.textBaseline = "middle";
ctx.fillText('Hello world', pt3.x, (pt3.y + pt4.y)/2);
<canvas id="canvas"></canvas>

I try to use the answer about recursion to draw a depth of 3 and 3 branches per tree,but answer is very strange?

I according the answer from use recursion to draw the tree and use recursion try to draw a depth of 3 and 3 branches per(like picture one)
but it print a strange answer (like picture three)
Please tell me where is the problem.THANK YOU.
picture one:
picture two: I use for to draw the answer
const tree = [
[0, 0, 0 ], // A // root
[-2, 104, -4 ], //B
[-39, 189, -23],//c
[-4, 230, 19 ], //D
[34, 192, -61 ], //E
[-150, 270, -72], //F
[-115, 296, -63], //G
[-106, 326, 6], //G
[-82, 365, 23 ],//I
[-43, 360, 123],//J
[10, 294, 144 ],//K
[76, 271, -176], //L
[132, 289, -150],//M
[87, 228, -63] //N
].map(v => new THREE.Vector3().fromArray(v));
// assumes there are 3 branches per
function addBranch(parent, depth, offset, tree, parentNdx = 0, childNdx = 1) {
const start = tree[parentNdx];
const end = tree[childNdx];
const length = start.distanceTo(end);
const material = new THREE.MeshPhongMaterial({color:'#ff0000'});
const geometry = new THREE.CylinderGeometry(5, 5, length, 20, 1, false);
geometry.translate(0, length / 2, 0);
geometry.rotateX(Math.PI / 2);
const mesh = new THREE.Mesh(geometry, material);
mesh.position.z = offset;
parent.add(mesh);
mesh.lookAt(end);
let ndx = childNdx + 1;
if (depth > 1) {
const numBranches = 3;
for (let i = 0; i < numBranches; ++i) {
ndx = addBranch(mesh, depth - 1, length, tree, childNdx, ndx);
}
}
return ndx;
}
addBranch(scene, 3, 0, tree)
picture three: strange answer
The algorithm uses depth first order, that means the order of your vertices needs to be
ABCFGHDIJKELMN
body {
margin: 0;
}
#c {
width: 100vw;
height: 100vh;
display: block;
}
<canvas id="c"></canvas>
<script type="module">
import * as THREE from 'https://threejsfundamentals.org/threejs/resources/threejs/r115/build/three.module.js';
function main() {
const canvas = document.querySelector('#c');
const renderer = new THREE.WebGLRenderer({canvas});
const fov = 75;
const aspect = 2; // the canvas default
const near = 1;
const far = 1000;
const camera = new THREE.PerspectiveCamera(fov, aspect, near, far);
camera.position.set(250, 500, -75);
camera.lookAt(0, 200, 0);
const scene = new THREE.Scene();
scene.background = new THREE.Color('lightskyblue');
{
const color = 0xFFFFFF;
const intensity = 1;
const light = new THREE.DirectionalLight(color, intensity);
light.position.set(1, 2, 4);
scene.add(light);
}
//ABCFGHDIJKELMN
const tree = [
[0, 0, 0], // A
[-2, 104, 4], // B
[-39, 183, -23], // C
[-156, 270, -72], // F
[-115, 296, -63], // G
[-106, 326, 6], // H
[-4, 230, 19], // D
[-82, 366, 23], // I
[-43, 360, 123], // J
[10, 294, 144], // K
[34, 192, -61], // E
[76, 271, -176], // L
[132, 289, -150], // M
[97, 228, -63], // N
].map(v => new THREE.Vector3().fromArray(v));
// assumes there are 3 branches per
function addBranch(parent, depth, offset, tree, parentNdx = 0, childNdx = 1) {
const start = tree[parentNdx];
const end = tree[childNdx];
const length = start.distanceTo(end);
const material = new THREE.MeshPhongMaterial({color:'#ff0000'});
const geometry = new THREE.CylinderGeometry(5, 5, length, 20, 1, false);
geometry.translate(0, length / 2, 0);
geometry.rotateX(Math.PI / 2);
const mesh = new THREE.Mesh(geometry, material);
mesh.position.z = offset;
parent.add(mesh);
mesh.lookAt(end);
let ndx = childNdx + 1;
if (depth > 1) {
const numBranches = 3;
for (let i = 0; i < numBranches; ++i) {
ndx = addBranch(mesh, depth - 1, length, tree, childNdx, ndx);
}
}
return ndx;
}
addBranch(scene, 3, 0, tree);
function resizeRendererToDisplaySize(renderer) {
const canvas = renderer.domElement;
const width = canvas.clientWidth;
const height = canvas.clientHeight;
const needResize = canvas.width !== width || canvas.height !== height;
if (needResize) {
renderer.setSize(width, height, false);
}
return needResize;
}
function render(time) {
time *= 0.001;
if (resizeRendererToDisplaySize(renderer)) {
const canvas = renderer.domElement;
camera.aspect = canvas.clientWidth / canvas.clientHeight;
camera.updateProjectionMatrix();
}
renderer.render(scene, camera);
// requestAnimationFrame(render);
}
requestAnimationFrame(render);
}
main();
</script>

How to define a heavy object in Matter JS

To explain better what I need, think about a tank and a can of soda.
No matter what the speed of that can of soda would be, the tank wouldn't move if it's hit.
But the tank should be able to move when force is applied to it.
Already opened an issue here with more details:
https://github.com/liabru/matter-js/issues/841
And I also made an example here for what I need.
var Engine = Matter.Engine,
Render = Matter.Render,
World = Matter.World,
Bodies = Matter.Bodies,
Body = Matter.Body;
var engine = Engine.create();
engine.positionIterations = 10;
engine.velocityIterations = 8;
var render = Render.create({
element: document.body,
engine: engine,
options: {
width: 800,
height: 400,
wireframes: false
}
});
engine.world.gravity.y = 0;
var topWall = Bodies.rectangle(400, 50, 720, 20, { isStatic: true });
var leftWall = Bodies.rectangle(50, 210, 20, 300, { isStatic: true });
var rightWall = Bodies.rectangle(750, 210, 20, 300, { isStatic: true });
var bottomWall = Bodies.rectangle(400, 350, 720, 20, { isStatic: true });
var ball = Bodies.circle(100, 200, 5, {
friction: 0.05,
frictionAir: 0.05,
density: 0.001,
restitution: 0.2
});
var bigBox = Matter.Bodies.rectangle(500, 200, 120, 120, {
inertia: Infinity,
frictionAir: 1,
friction: 1,
density: 0.1
});
World.add(engine.world, [topWall, leftWall, rightWall, bottomWall, ball, bigBox]);
Engine.run(engine);
Render.run(render);
// CMD + SHIFT + 7 to reload
$('.shoot').on('click', function () {
var speed = 0.02;
var angle = 0;
let vector = Matter.Vector.create(Math.cos(angle) * speed, Math.sin(angle) * speed);
Body.applyForce(ball, ball.position, vector);
});
https://codepen.io/dmerlea/pen/BaoQJYK

Getting a Black Screenshot with BabylonJS

I am having some trouble taking a screenshot. If you followed along with the tut, at: http://doc.babylonjs.com/tutorials/render_scene_on_a_png you see that they only provided one line which is BABYLON.Tools.CreateScreenshot(engine, camera, size);
With size and your camera being the variables that you can change. When I implemented this, I would get a black screenshot. I first thought that maybe the it was taking a screenshot before the page rendered so I added a simple loop to and added an alert box to wait until the scene loaded before the screenshot would execute. But for some reason I am still getting a black screenshot.
Thank you for your input :D
var canvas = document.querySelector("#renderCanvas");
var engine = new BABYLON.Engine(canvas, true);
//Needed for the CreateScene Function
var createScene = function () {
var scene = new BABYLON.Scene(engine);
// Setup camera
var camera = new BABYLON.ArcRotateCamera("Camera", 0, 10, 0, BABYLON.Vector3.Zero(), scene);
camera.setPosition(new BABYLON.Vector3(-10, 10, 25));
camera.attachControl(canvas, true);
// Lights
var light0 = new BABYLON.PointLight("Omni0", new BABYLON.Vector3(0, 10, 5), scene);
var light1 = new BABYLON.PointLight("Omni1", new BABYLON.Vector3(0, -10, 5), scene);
var light2 = new BABYLON.PointLight("Omni2", new BABYLON.Vector3(10, 0, 5), scene);
var light3 = new BABYLON.DirectionalLight("Dir0", new BABYLON.Vector3(1, -1, 2), scene);
var light4 = new BABYLON.SpotLight("Spot0", new BABYLON.Vector3(0, 5, -10), new BABYLON.Vector3(0, -1, 0), 0.8, 3, scene);
var light5 = new BABYLON.HemisphericLight("Hemi0", new BABYLON.Vector3(0, 1, 0), scene);
var material = new BABYLON.StandardMaterial("kosh", scene);
var sphere = BABYLON.Mesh.CreateSphere("Sphere", 16, 3, scene);
var cylinder = BABYLON.Mesh.CreateCylinder("cylinder", 7.5, 3, 6, 6, 1, scene);
var box = BABYLON.Mesh.CreateBox("box", 6.0, scene);
// Creating light sphere
var lightSphere0 = BABYLON.Mesh.CreateSphere("Sphere0", 16, .5, scene);
var lightSphere1 = BABYLON.Mesh.CreateSphere("Sphere1", 16, 0.5, scene);
var lightSphere2 = BABYLON.Mesh.CreateSphere("Sphere2", 16, 0.5, scene);
//Shifting position up of Sphere
sphere.position.y = 5;
box.position.y = -2;
//generating shadows
var shadowGenerator = new BABYLON.ShadowGenerator(1024, light3);
shadowGenerator.getShadowMap().renderList.push(box);
shadowGenerator.getShadowMap().renderList.push(sphere);
shadowGenerator.getShadowMap().renderList.push(cylinder);
//Colors
lightSphere0.material = new BABYLON.StandardMaterial("red", scene);
lightSphere0.material.diffuseColor = new BABYLON.Color3(0, 0, 0);
lightSphere0.material.specularColor = new BABYLON.Color3(0, 0, 0);
lightSphere0.material.emissiveColor = new BABYLON.Color3(1, 0, 0);
lightSphere1.material = new BABYLON.StandardMaterial("green", scene);
lightSphere1.material.diffuseColor = new BABYLON.Color3(0, 0, 0);
lightSphere1.material.specularColor = new BABYLON.Color3(0, 0, 0);
lightSphere1.material.emissiveColor = new BABYLON.Color3(0, 1, 0);
lightSphere2.material = new BABYLON.StandardMaterial("blue", scene);
lightSphere2.material.diffuseColor = new BABYLON.Color3(0, 0, 0);
lightSphere2.material.specularColor = new BABYLON.Color3(0, 0, 0);
lightSphere2.material.emissiveColor = new BABYLON.Color3(0, 0, 1);
// Sphere material
material.diffuseColor = new BABYLON.Color3(1, 1, 1);
sphere.material = material;
// Lights colors
light0.diffuse = new BABYLON.Color3(1, 0, 0);
light0.specular = new BABYLON.Color3(1, 0, 0);
light1.diffuse = new BABYLON.Color3(0, 1, 0);
light1.specular = new BABYLON.Color3(0, 1, 0);
light2.diffuse = new BABYLON.Color3(0, 0, 1);
light2.specular = new BABYLON.Color3(0, 0, 1);
light3.diffuse = new BABYLON.Color3(1, 1, 1);
light3.specular = new BABYLON.Color3(1, 1, 1);
light4.diffuse = new BABYLON.Color3(1, 0, 0);
light4.specular = new BABYLON.Color3(1, 1, 1);
light5.diffuse = new BABYLON.Color3(1, 1, 1);
light5.specular = new BABYLON.Color3(1, 1, 1);
light5.groundColor = new BABYLON.Color3(0, 0, 0);
//Adding the SkyBox
var skybox = BABYLON.Mesh.CreateBox("skyBox", 100.0, scene);
var skyboxMaterial = new BABYLON.StandardMaterial("skyBox", scene);
skyboxMaterial.backFaceCulling = false;
skyboxMaterial.reflectionTexture = new BABYLON.CubeTexture("../textures/TropicalSunnyDay", scene);
skyboxMaterial.reflectionTexture.coordinatesMode = BABYLON.Texture.SKYBOX_MODE;
skyboxMaterial.diffuseColor = new BABYLON.Color3(0, 0, 0);
skyboxMaterial.specularColor = new BABYLON.Color3(0, 0, 0);
skyboxMaterial.disableLighting = true;
skybox.material = skyboxMaterial;
// Animations
var alpha = 0;
scene.beforeRender = function () {
light0.position = new BABYLON.Vector3(10 * Math.sin(alpha), 0, 10 * Math.cos(alpha));
light1.position = new BABYLON.Vector3(10 * Math.sin(alpha), 0, -10 * Math.cos(alpha));
light2.position = new BABYLON.Vector3(10 * Math.cos(alpha), 0, 10 * Math.sin(alpha));
lightSphere0.position = light0.position;
lightSphere1.position = light1.position;
lightSphere2.position = light2.position;
lightSphere0.position.y = 5;
lightSphere1.position.y = 5;
lightSphere2.position.y = 5;
alpha += 0.01;
};
//ground
var ground = BABYLON.Mesh.CreateGround("ground1", 100, 100, 2, scene);
ground.receiveShadows = true;
var materialGround = new BABYLON.StandardMaterial("grassTexture", scene);
materialGround.diffuseColor = new BABYLON.Color3(1,1,1);
materialGround.diffuseTexture = new
BABYLON.Texture("../textures/grass.png",scene);
ground.material = materialGround;
//wait loop for the screenshot
size = { width: 600, height: 400};
var i = 1;
function myLoop () {
setTimeout(function () {
alert('Taking Screenshot!');
//Creating png screenshot
BABYLON.Tools.CreateScreenshot(engine, camera, size);
i++;
if (i < 1) {
myLoop();
}
}, 2000)
}
myLoop();
//Returning the scene
return scene;
};
var scene = createScene();
engine.runRenderLoop(function () {
scene.render();
});
window.addEventListener("resize", function () {
engine.resize();
});
The cool fellow (DeltaKosh) over at htmlgameDevs helped me out. He said that when you are creating an engine for a Screenshot or rendering a PNG file that you must define it this way:
engine = new BABYLON.Engine(canvas, true, { preserveDrawingBuffer: true, stencil: true });
I also added a loop that waited a second until after the scene renders to take the screenshot
var scene = createScene();
var booleanR = false;
var size = 512;
engine.runRenderLoop(function () {
scene.render();
});
function myLoop () {
setTimeout(function () {
if(booleanR == false){
BABYLON.Tools.CreateScreenshot(engine, camera, size);
booleanR = true;
}
}, 1000)
}
This is the link from his response: http://www.html5gamedevs.com/topic/32765-getting-a-black-screenshot-with-babylonjs/?tab=comments#comment-187819

Resources