I want to rotate the whole SVG screen using a mouse click on the middle of the screen or a button click. The idea is, when I will click the button using the mouse, my whole screen will rotate 90 degrees.
I have found many examples in the stackoverflow where a particular box or arrow is rotated. But I want to rotate the full screen.
Any working example would be great.
Category: javascript
Category Added in a WPeMatico Campaign
Moment.js formatting date field to a day earlier than it should
This is a very small little bug I’m trying to fix in my code. I have a React component with this initial state
const initialFormData = Object.freeze({
date: Moment(new Date()).format('YYYY-MM-DD'),
price_per_gallon: '',
trip_distance: '',
gallons: '',
car: ''
});
const [formData, updateFormData] = useState(initialFormData);
Which is used in a form like so:
<MuiPickersUtilsProvider utils={DateFnsUtils}>
<Grid container justifyContent="space-around">
<KeyboardDatePicker
fullWidth
disableToolbar
inputVariant="outlined"
format="yyyy-MM-dd"
margin="normal"
id="date-picker-inline"
label="Date of Fillup"
name="date"
value={formData.date}
onChange={handleDateChange}
KeyboardButtonProps={{
'aria-label': 'change date',
}}
/>
</Grid>
</MuiPickersUtilsProvider>
I’ve done console.logs to see that the Moment(new Date()).format(‘YYYY-MM-DD’) shows today’s date, as I want, but for some reason when the component renders it has the default date in the field as yesterday
, but if I were to get rid of initializing the date with the Moment instance and just have it be ‘new Date()’, it renders with today’s date correctly.
Any idea why this could be? Kind of wracking my brain here, I just want the default date on my form to be whatever the current day is, and need to format it to YYYY-MM-DD because that’s how my API sends and receives the data
How do I make an iframe click counter
I want to make a Iframe where when I click into it it updates a number that is displaying below it. Is this even possible?
How to filter value from array of object [duplicate]
I have json data. How to filter item with tags = “chair” using node.js from json data :
const data = [
{
"inventory_id": 9382,
"name": "Brown Chair",
"type": "furniture",
"tags": [
"chair",
"furniture",
"brown"
],
"purchased_at": 1579190471,
"placement": {
"room_id": 3,
"name": "Meeting Room"
}
}
]
Calculating the total and average of marks using Javascript and HTML
I am new into Javascript btw thus I really hope someone can help me point out my mistake. I have done all the things that the question asked except for the calculation for total and average part. I got it wrong and I dont know what went wrong. Example marks I have keyed in were 1,2,3,4. Supposed to be the total and average should be 10 and 2.5 but it displayed 1234 and 308.5. Here`s what I have tried :
```
<!DOCTYPE html>
<html>
<head>
<title>MARK</title>
</head>
<body>
<script src="mark.js"></script>
<form>
Mark : <input type="text" id="Mark1" /><br>
Mark : <input type="text" id="Mark2" /><br>
Mark : <input type="text" id="Mark3" /><br>
Mark : <input type="text" id="Mark4" /><br>
<br>
<input type="button" onClick="findTotal()" Value="Find Total" />
<input type="button" onClick="findAverage()" Value="Find Average" />
</form>
<br>
Result : <br>
<span id = "result"></span>
</body>
</html>
```
<pre><code>
function findTotal()
{
var num1 = document.getElementById("Mark1").value;
var num2 = document.getElementById("Mark2").value;
var num3 = document.getElementById("Mark3").value;
var num4 = document.getElementById("Mark4").value;
document.getElementById("result").innerHTML = num1 + num2 + num3 + num4;
}
function findAverage()
{
var num1 = document.getElementById("Mark1").value;
var num2 = document.getElementById("Mark2").value;
var num3 = document.getElementById("Mark3").value;
var num4 = document.getElementById("Mark4").value;
document.getElementById("result").innerHTML = (num1 + num2 + num3 + num4) / 4;
}
</code></pre>
Auto rotation for images not working on flutter app
I am using html and css to display a webview in an iOS app written in swift and an Android app written in Flutter. The image autorotates in browsers and the iOS app but not in the Android app. I would like the images to autorotate to being rightside up using html, css, and/or js. How could I accomplish this? Thanks!
Code:
<!DOCTYPE html>
<html>
<style>
#bodystyle {
position: absolute;
}
#image {
width: 100%;
height: 100%;
}
</style>
<body id="bodystyle" style="background-color: #000;">
{%for i in range(images)%}
<img id='image' src="{{ images[i] }}" alt="Image for user"></img>
{%endfor%}
</body>
</html>
Rotate object in div towards direction of mouse click
I have an image of an arrow that rotates around a fixed central point and I want to make it to where it moves in the direction of a mouse click. So someone can click somewhere on the screen and the arrow will point in the direction of the mouse click.
This is what the arrow looks like:
And this is what the code of the div looks like
content.innerHTML+= '<img class = "arrow" style="transform: rotate('+x+'deg)" src="img/arrow.png"</img>'
and the CSS for those interested:
.arrow {
width: 200px;
height: 10px;
position: absolute;
transform-origin: 100% 50%;
}
Any pointers appreciated! Thanks!
why is await not waiting and returns promise object? how to handle multiple maps?
i have 2 .maps and returns updated value.
ive done a lot of research about multiple .map promises but i just couldn’t get it.
export async function ToBE(workspace: any) {
let components: any = []
components = await loopThroughMaincomponents(workspace, components)
components = await loopThroughMaincomponents_Then_Components(workspace, components)
return {
id: workspace.id,
body: components,
name: workspace.workspace_name,
description: `${workspace.workspace_name} Application`,
created_by: "Master",
};
}
This is the main problem.
the components return is:
(3) [Array(3), Array(3), Array(3)]
0: (3) [Promise, Promise, Promise]
1: (3) [Promise, Promise, Promise]
2: (3) [Promise, Promise, Promise]
length: 3
[[Prototype]]: Array(0)
Reference for the functions:
const loopThroughMaincomponents = async (workspace: any, components: any) => {
return workspace.components.map(async (workspace_body: any, key: number) => {
if (workspace_body.type === "template" || workspace_body.type === "main") {
let sub_component_ids: any = []
const template_id = key + 1
components.push({
"id": template_id, // Template ID
"templateId": workspace_body.id,
"type": "main",
"label": workspace_body.data.name,
"name": workspace_body.data.name,
"description": "----",
"image_uri": "---",
"properties": workspace_body.data
})
const subcomponents = await loopThroughSubcomponents(workspace_body.data.subcomponents, components, template_id, sub_component_ids)
components = await updateSubComponentIds(subcomponents.components, subcomponents.sub_component_ids, template_id)
}
return components
})
}
const loopThroughMaincomponents_Then_Components = async (workspace: any, components: any) => {
return workspace.components.map((workspace_body: any) => {
if (workspace_body.type === "bidirectional") {
console.log("components", components)
console.log("workspace_body", workspace_body)
// It will update components
return components.map((node: any) => {
if (node.id === workspace_body.source) {
let newcon = {
id: workspace_body.id,
type: workspace_body.type,
source: workspace_body.source,
target: workspace_body.target,
sourceHandle: workspace_body.sourceHandle,
targetHandle: workspace_body.targetHandle,
arrowHeadType: workspace_body.arrowHeadType
}
let mix = {
...node.properties.connection,
...newcon
}
console.log("mix", mix)
mix = {
...node.properties,
properties: mix
}
return {
...node,
...mix
}
}
return node
})
}
return components
})
researched:
Async function returning promise, instead of value
Async/Await not waiting for response, and returns Promise object
Async/Await not waiting
Writing multiple map functions in Promise.all()
.map is not getting re-rendered after updating the state
I have fetched data using useEffect, and also getting the data on console. I’ve called the setState method to update the state, but the .map function is not working. Even on the console nothing is happening.
import React, { useState, useEffect } from "react";
function HomePage() {
const [isFlipped, setIsFlipped] = useState(false);
const [cardData, setCardData] = useState([]);
// useEffect function
useEffect(async () => {
const url = "https://rickandmortyapi.com/api/character";
const fetchData = async () => {
try {
const response = await fetch(url);
const json = await response.json();
const jsonData = json.results;
setCardData(jsonData);
console.log(jsonData);
} catch (error) {
console.log("error", error);
}
};
fetchData();
}, []);
const handleClick = () => {
setIsFlipped(!isFlipped);
};
return (
<>
{cardData.map((c) => {
<h1>{c.id}</h1>;
})}
</>
);
}
export default HomePage;
How to add icon to autocomplete in PrimeVue
I’ve run into this issue. There is a feature request for this at the moment.
I’ve provided my workaround in the answer below.
Leetcode – how to optimize my solution using sliding window approach for maximum number of vowels in a substring of k length?
I am doing leetcode 1456
I came up with a working solution but got “Time limit exceeded” for one of the tests. I used the sliding window approach and I believe my solution is O(n) (or am I wrong?).
var maxVowels = function(s, k) {
let left = 0;
let right = 0;
let maxVowels = 0;
let curVowels = 0;
let vowels = ['a','e','i','o','u'];
while (right < s.length) {
let subStringLength = right - left + 1;
if (vowels.includes(s[right])) curVowels++;
if (subStringLength === k) {
maxVowels = Math.max(maxVowels, curVowels);
if (maxVowels === k) return maxVowels;
curVowels = 0;
left++;
right = left - 1;
}
right++;
}
return maxVowels;
};
I tried to see if it was because the vowels.includes(s[right]) method was somehow a really slow method but based on what I read that is not the case, especially since my array is only of length 5.
How can I optimize my solution such that it passes the test?
export ‘useRifm’ (imported as ‘useRifm’) was not found in ‘rifm’ (possible exports: Rifm)
Compiled with problems:X
ERROR in ./node_modules/@mui/lab/internal/pickers/hooks/useMaskedInput.js 64:20-27
export ‘useRifm’ (imported as ‘useRifm’) was not found in ‘rifm’ (possible exports: Rifm)
getting this error when I try to import import TimePicker from ‘@mui/lab/TimePicker’;
import React from "react";
import "react-datepicker/dist/react-datepicker.css";
import ReactDatePicker from "react-datepicker";
import { motion, AnimatePresence, animate } from 'framer-motion'
import './appointment.css';
import { useForm, Controller } from "react-hook-form";
import TextField from '@mui/material/TextField';
import AdapterDateFns from '@mui/lab/AdapterDateFns';
import LocalizationProvider from '@mui/lab/LocalizationProvider';
import TimePicker from '@mui/lab/TimePicker';
Using a Loop To Add/Remove Event Listeners [duplicate]
I have a grid with X clickable areas. Once clicked I push the id into usedArr. I want to use removeEventListener on all id’s that are in array while still being able to have the remaining areas clickable.
for (var i = 0; i < usedArr.length; i++) {
temp = usedArr[i]
temp.removeEventListener("click", play);
};
OR
usedArr.forEach(function(arrayElement) {
arrayElement.removeEventListener("click", play);
});
Neither of these work.
Can someone explain to me the rule in JavaScript that applies here?
So I was playing around with JavaScript, and I ran into a problem. I was trying to make two buttons change the written text above them, with the change being different depending on what was already written. There were only 4 potential options. So what I thought was to just use an if and else ifs to make it work. But it didn’t work. Then over the next couple of hours I went over it again and again and couldn’t make it work. Finally I came up with an idea that made it work but I don’t know why.
The above is very long so you if you’d rather skip it, just read this:
Why does a) work and not b)?
First my HTML:
<html>
<head>
<meta charset="utf-8">
<title>Test</title>
<link rel="stylesheet" type="text/css" href="stylesheets.css">
<script rel="javascript" src="script.js"></script>
</head>
<body onload="boopFunction()">
<h1 id="boop"></h1>
<button onclick="ggFunction()">Leave
</button>
<button onclick="eeFunction()">Enter</button>
</body>
</html>
Then what worked, a):
var Boop = document.getElementById("boop");
/* onload welcome */
function boopFunction() {
document.getElementById("boop").innerHTML = Poob[4];
Boop = Poob[4];
}
/* leave button */
function ggFunction() {
if (Boop == Poob[0]) {
document.getElementById("boop").innerHTML = Poob[1];
Boop = Poob[1];
}
else if (Boop == Poob[2]) {
document.getElementById("boop").innerHTML = Poob[1];
Boop = Poob[1];
}
else if (Boop == Poob[1]) {
document.getElementById("boop").innerHTML = Poob[3];
Boop = Poob[3];
}
else if (Boop == Poob[4]) {
document.getElementById("boop").innerHTML = Poob[1];
Boop = Poob[1];
}
}
/* enter button */
function eeFunction() {
if (Boop == Poob[1]) {
document.getElementById("boop").innerHTML = Poob[0];
Boop = Poob[0];
}
else if (Boop == Poob[3]) {
document.getElementById("boop").innerHTML = Poob[0];
Boop = Poob[0];
}
else if (Boop == Poob[0]) {
document.getElementById("boop").innerHTML = Poob[2];
Boop = Poob[2];
}
else if (Boop == Poob[4]) {
document.getElementById("boop").innerHTML = Poob[0];
Boop = Poob[0];
}
}
Lastly what did not work, b):
var Poob = ["Welcome!", "Goodbye!", "You're still here?", "You're still outside!", "Hello there!"];
var Boop = document.getElementById("boop");
/* onload welcome */
function boopFunction() {
document.getElementById("boop").innerHTML = Poob[4];
}
/* leave button */
function ggFunction() {
if (Boop == Poob[0]) {
document.getElementById("boop").innerHTML = Poob[1];
}
else if (Boop == Poob[2]) {
document.getElementById("boop").innerHTML = Poob[1];
}
else if (Boop == Poob[1]) {
document.getElementById("boop").innerHTML = Poob[3];
}
else if (Boop == Poob[4]) {
document.getElementById("boop").innerHTML = Poob[1];
}
}
/* enter button */
function eeFunction() {
if (Boop == Poob[1]) {
document.getElementById("boop").innerHTML = Poob[0];
}
else if (Boop == Poob[3]) {
document.getElementById("boop").innerHTML = Poob[0];
}
else if (Boop == Poob[0]) {
document.getElementById("boop").innerHTML = Poob[2];
}
else if (Boop == Poob[4]) {
document.getElementById("boop").innerHTML = Poob[0];
}
}
Can anyone explain why b) didn’t work and a) did? Why I had to add Boop = Poob[]?
React: trigger an event or value outside the root component
Somebody could help me with this idea? I don’t know if it were possible or should I use any library or skill.
Component (This is usually and common)
import React, { useState } from "react";
export default function App() {
const [anyVal, setAnyVal] = useState(0);
return (
<div className="App">
<h1>Any Value: {anyVal}</h1>
<button onClick={() => setAnyVal(anyVal + 1)}>Change value</button>
</div>
);
}
Connection HTML + React Dom (This is usually and common)
import { StrictMode } from "react";
import ReactDOM from "react-dom";
import App from "./App";
const rootElement = document.getElementById("root");
ReactDOM.render(
<StrictMode>
<App />
</StrictMode>,
rootElement
);
Here the part that I don’t know if it were possible
<body>
<div id="root" onanyvaluechanged="externalFn(v)"></div>
<script>
function externalFn(newValue) {
alert("I've detected the change:" + newValue);
}
</script>
</body>