www.html5rocks.com | .... a top-notch resource for web developers | Awesome List library
kandi X-RAY | www.html5rocks.com Summary
kandi X-RAY | www.html5rocks.com Summary
....a top-notch resource for web developers
Support
Quality
Security
License
Reuse
Top functions reviewed by kandi - BETA
Currently covering the most popular Java, JavaScript and Python libraries. See a Sample of www.html5rocks.com
www.html5rocks.com Key Features
www.html5rocks.com Examples and Code Snippets
Community Discussions
Trending Discussions on www.html5rocks.com
QUESTION
I'm really kind of struggling at the moment and I don't know how hard it can possibly be. I followed several different tutorials and answers here on SO and YouTube, but all are doing it differently and I am kind of stuck right now.
All I want is a custom HTML-Tag
with some custom attributes
I tried it with this approach (JS)
...ANSWER
Answered 2021-Jun-07 at 13:51I ran your code and it worked just fine with the constructor
, but you could try connectedCallback
instead. This method gets called any time an element is added to the document.
Make sure your script runs in the of the document, before your
is executed. HTML/scripts run top-to-bottom. Your element needs to be registered before it is rendered.
Update: Please refer to the "Custom elements" - JavaScript.info article that was linked by Martin Braun for a better understanding. The official MDN documentation can be viewed here: "Using custom elements" - MDN.
QUESTION
Trying in JS to create a single oscillator, with a smooth, non-clicking frequency tone that rapidly turns on and off. Timing for web audio discussed by [Chris Wallace][1]. A timing scheduler can be found by [Aqilah Misuary][2]. This example produces a correct-sounding tone based on the frequency. However, a noticeable click is present. Fixing clicks has been addressed by a posting on [Stack Overflow][3].
First code snippet is based on the Code Pen version (see link 2), changing frequency to 432 Hz.
Second code snippet: Adapted JS (see links 2 and 3) from these resources, adding ramping, using the 432 Hz frequency,I can get the clicking to stop, but my frequency sound is now blunted, no longer has the correct 432 Hz frequency sound. I suspect that I am doing something wrong, not using the timing or settings correctly. I've tried various timings and settings and even "initial" instead of "exponential" ramping, but none have fixed the issue: clicking gone but frequency sound is now blunted, doesn't retain the correct sound based on the frequency chosen and that would be heard if ramping is not used, leaving the clicking. I must be doing something wrong?
Any way to fix the clicking without blunting the sound? [1]: https://www.html5rocks.com/en/tutorials/audio/scheduling/ [2]: https://codepen.io/aqilahmisuary/pen/ONEKVM [3]: Web audio "Click" sound even when using exponentialRampToValueAtTime
...ANSWER
Answered 2021-Apr-13 at 12:48Based on the comments, we're essentially dealing with a short repetitive loop that needs exact timing, so an AudioBufferSourceNode is our weapon of choice:
It's especially useful for playing back audio which has particularly stringent timing accuracy requirements, such as for sounds that must match a specific rhythm
Unfortunately that also means we'll need to get our hands dirty and write some DSP code to synthesize that buffer, but it's honestly not that bad (especially since we can just work in floating point numbers, not raw PCM buffers...).
To avoid clicks (where the oscillator is cut off mid-phase, as it were), we take advantage of the facts that a sine wave always starts at zero and we render a single loop of the sound, so all we need to do is make sure the end of the wave does not stop abruptly. We do that by slightly adjusting the length of the tone to make sure the last audible sample is very near to zero.
The example here has a couple of buttons to demonstrate different parameters. You could hook those up into UI components.
QUESTION
I'm making a game made of bare JavaScript code and I want to manipulate audio in JavaScript.
Specifically, I want to let the page start playing music when some div
element is clicked and when using some function in the JavaScript code.
I know audio tag of HTML5 has a limitation that a music file associated with Audio
element cannot be played without user clicking the play button of the audio element, so I cannot do like this:
ANSWER
Answered 2021-Mar-15 at 13:13You're correct. You can only be sure that the playback will not be blocked by the autoplay policy if the playback is started in response to a user gesture. A button with a click handler is the classic example but the click handler could be attached to a div as well. Let's say your div has an id called my-div
. In that case the following should work.
QUESTION
I programmmed a website which uses text to speech engine to generate audio files.
Then these mp3 files are started using Web Audio API.
Everything works fine when hearing aufio from speakers on a computer or on a smart phone.
However, as soon as I connect my bluetooth helmet to the smart phone, the audio is not played.
Is it a famous issue that Web Audio API doesn't work with bluetooth devices, or does the issue come from my code?
Do I need to change the context's destination ? How can I set it to buetooth? (ex : https://www.html5rocks.com/en/tutorials/webaudio/intro/)
...ANSWER
Answered 2021-Jan-11 at 02:59Hi use the web bluetooth library for more reference read this here
QUESTION
I am using fancybox 3, there is no problem, the thing I want to do is to click on "card-body" and I want the relevant "card" to be opened. I have a situation like that I do not want to be repeated fancybox.
It works in the link I gave below, but it doesn't work when I do.
...ANSWER
Answered 2020-Nov-08 at 13:04When the body is clicked, you want the relevant card description, I think you can solve it this way.
QUESTION
I found this guide showcasing image filters/effects using canvas and JavaScript: https://www.html5rocks.com/en/tutorials/canvas/imagefilters
Though, I don’t have any idea on how to implement it. I’m not good with JavaScript (I’m a graphic designer), so I’m not even understanding the syntax. I extracted this code from the page, but I don’t know if it’s complete or how to use it:
...ANSWER
Answered 2020-Sep-15 at 01:30If you want to apply this filter on an HTML
Instead look at CSS filters for the simple cases, or SVG filters for the more complex ones.
The one you want is a simple one and can be achieved with CSS filters only:
QUESTION
I tried searching for an answer but could not find a viable one.
Is there a simple way to position the subtitles below the HLML5 video frame?
In other words, I do not want the subtitles / captions to appear inside the video frame itself. I want them to appear approximately 1/2 inch below the video frame.
Currently the subtitles are in a standard .srt file, but I can adapt the subtitles to a method that works for this.
I am also currently using HTML element.
The image below represents what I want to accomplish.
There is a video example here Example by Sam Dutton, but the code he references to Eric Bidelman is a dead link.
On the example page Sam Dutton shows snippets of code, but I can make anything out of it.
Play the example video on the example page to see what I am talking about.
Thank you in advance.
...ANSWER
Answered 2020-May-22 at 05:35Well, I looks like I am answering my own question.
Since searching the web for a possible solution yielded no viable results, I went back and spent a few hours working on the video example on Sam Dutton's page.
From this, I was able to put together a simplified HTML5 video solution that can reposition the subtitles above or below the video as shown the posted image above.
Below is the complete HTML code for two solutions.
(1) Simplified Solution - A minimum code solution that works well.
(2) Full Solution - Yields the same results found on Sam Dutton's page.
The solutions include .webm video and .vtt subtitle files. MP4 video and .srt subtitles work too.
Note: The HTML code has to reside on a web server to run properly. It does not function properly on a non-server type local machine. Just save the code in an HTML file (.html) and upload it to a web server.
Below is the Debug Security notice presented when trying to open the .html file locally within the Chrome browser:
QUESTION
This may seem like a strange question but I'm trying to understand what exactly my code is doing memory wise in order to make it as efficient as possible.
When a database request is made such as req = objectStore.getAll( keyRange );
and the IDBRequest
object is returned and the result provided later to the result
property of the object, where is the object created? Is it just like any other JS object, allocated and released by the GC; and the variable req
is just a reference to it, such that once the reference is broken the GC 'knows' the object is unreachable and releases the memory?
If many such requests are made over a short interval of time, is there a way to not consume additional RAM for each result?
For example, the process I'm interested in is a button click invokes two promises(one write and one read) through a Promise.allSettled
, one that writes the current state to the database, and another that retrieves new data and builds a document fragment from it. If both fulfill, the fragment replaces a node in the DOM.
If a user rapidly clicks through this data, every read/getAll
returns an IDBRequest
with a result that is an array of objects, and that appears to consume more and more RAM until the GC runs. I can observe that the memory is eventually released, but was wondering if there is a way to not have that happen. Since I know what those objects are in terms of maximum size, could I write the IDBRequest to an exsiting object that is like a template object and only ever need one or two of them, such as one for current and one for the new request, rather than continually adding new objects until the GC releases those deemed unreachable?
Thank you for considering my question.
Thanks for the answer concerning where the IDBRequest object is allocated and advice concerning avoidance of memory leaks. Just to add further explanantion of what I was observing and wondering if it were possible, I'm adding this note.
There isn't a single global variable declared in my code, all exist within functions or are properties of a function object; and I set tem to null at the close of every function just in case I missed some scope/closure hidden reference. After finally getting a large portion of I/O within indexedDB working, I started to consider what would happen as a user worked within my application for an hour or two. Would memory use continually increase in the long term, even though I observed no issues during all the building and testing?
I filled the database with 500 data packets, meaning it takes more than one DB object to build a new DOM node; it is anywhere from 15 to 60 objects per node, depending upon what the user builds. So, I made every one of the 500 packets to be comprised of 60 objects and made those objects overly large in size for the testing, far greater than expected during appropriate use of the tool.
Then through a setInterval, the save-state, get-and-build promises were invoked every 500ms from packet 1 to 500; and I observed the data usage at the maro level only. The result appears to be that at any one time, there can be about one hundred of these packets in RAM between the GC runs. As the packets are retrieved, and nodes built and replaced, the RAM usage steadily increases and drops about five times during the traversal from packet 1 to 500. The max level of each increase prior to the drop is a little higher than the previous one. About 45 seconds or so after completion, the memory returns to just about where it was when the setInterval commenced.
Thus, fortunately, I don't think there is a memory leak. However, RAM usage is much as described in this article about using object pools. I'm interested in the graphs under the heading "Reduce Memory Churn, Reduce Garbage Collection taxes"--that saw-tooth pattern that consumes far more memory than is ever needed at any one time, when it could be like the second graph that is smaller, level, and requires fewer GC runs in total.
And the first answer to this SO question, at almost the very end, writes that this causes the GC to have to trace more objects also.
I'm not sure if the GC will run at a lower total RAM consumption or will wait until some maximum level is reached. Of course, I can test that on my machine, but that isn't very definitive overall.
I'm not building a game and a pause for a GC run isn't an issue; and a user should never click through 500 data packets in 250 seconds total and there will never be 500 packets of such a ridiculous size. Perhaps, that test was unrealistic; but the objective was to attempt to exacerbate the effect of using the tool for an extended period of time and generating many, many small objects throughout. Even a get/put for a minor edit creates a new object each time. These are concepts that I hadn't considered before and was just focused on how to get the I/O working accurately first.
When you consider how many objects sit around in RAM for a little while at least, waiting to be garbage collected, it seems reaonsable to simply hold the current packet at all times, such that a get
operation is not required for an edit. Just edit the object in RAM and use a put
operation only. Then all those get
request result objects for edits are never created. But that doesn't eliminate the need for objects to hold the newly requested full data packets.
I understand that the browser's GC process is supposed to make all of this easier but it seems that, by doing so, it takes a lot out of the coders' control; and the advice that I see on SO in other questions is usually to not worry about it unless you experience an issue. I'm just an amateur at best but I'd prefer to understand what is happening in the background and code from the start with that in mind; and perhaps there is some stubbornness on my part that, regardless of how powerful the processor and size of RAM, my little tool ought to use as little resources as necessary or I haven't done my job.
I don't know if an object pool is a good technique anyway, but, even if it were, it appears that it would be an impossibility when it comes to retrieving data from indexedDB because the IDBRequest object is always created anew and could never be written to an existing object.
Thanks again for the explanation.
...ANSWER
Answered 2020-Apr-26 at 17:04The result property of the IDBRequest object holds the data in memory just like any other object. When nothing references the request object the memory is reclaimable. There is no way to not consume additional memory for each new result. An allocation is a memory acquisition.
Chrome's policy is that keeping things around in virtual memory is not a problem until there is contention. You should not concern yourself with excessive memory usage until there is proof that it causes a performance impact. Most of the time it does not.
You can, however, look for where in your code you retain references to request objects. If you keep references to them around forever, then the objects will never be released and are not reclaimable. Very much like the old IE bug with event listeners, a form of a memory leak.
A surefire novice proof way of avoiding this behavior is to just use variables in functions and not global variables. Per function call variables are generally reclaimable at scope exit, when the function call completes, and there isn't much thought involved, and there isn't explicit code that tries to micromanage something already managed for you. For example, there is no need to declare all variables as let
instead of const
and set value = undefined;
or delete value;
after every variable use. So I would look at your code and look at where you are retaining references to variables beyond the lifetime of the function that created them. Those are the culprits.
QUESTION
I understand that memory allocation and release is controlled by the browser; but can allocated memory ever be programmatically re-used in JavaScript?
For example, suppose a function (getData) property (data) is used to store the results of an indexedDB getAll
request, as ( I realize you'd never write code like this)
getData.data = objectStore.getAll( keyRange ).result;
Can subsequent invocations of getData
re-use the memory already allocated from the previous invocation or will the browser always allocate a new area of memory to hold the result and just point getData.data
to it, and later release the memory that held the previous results?
Thank you.
A reason for asking is from observing how memory is used in my application. This getData-type function may be invoked several hundred times in a user session and, as it is now, the RAM consumed continues to increase until a certain point is reached and then it is released. That is how the GC is supposed to work, I realize, but if the already allocated memory could be re-used, the applciation would never need to consume that much RAM at any given point.
I don't think there is a way to accomplish what I was considering because indexedDB will always allocate memory when it retrieves the objects from the object store. The getAll
request will return a request object, the result of which will be an array of objects and, even if it were possible to write that result to an already exisiting array or object, nothing would be gained. All my code really only references or points to the result of the request; and the temporary increasing use of RAM usage will always continue until the GC runs, because the database request cannot be directly written to a specific area of memory from its 'creation' so to speak. The request object, including the result, will exist somewhere in RAM before object pooling or any other approach could be attempted.
The testing I mentioned in the comments was unrealistic, being comprised of 500 invocations of getData
separated by only 500ms per invocation and the data retrieved in each invocation was far larger than would ever exist in the application under expected use. So, RAM usage had time to increase significantly before the GC ran. The RAM usage rose and fell several times as the 500 invocations processed in turn, each episode, I assume, being a run of the GC. I like the flat RAM usage graph in the object pooling article here which minimizes GC runs; but it doesn't appear achievable when using indexedDB.
ANSWER
Answered 2020-Apr-24 at 16:02Can allocated memory ever be programmatically re-used in JavaScript?
Yes, through object pooling. However, this is hardly ever reasonable, the native allocator/garbage collector will outperform this approach in 99% of the cases.
QUESTION
In the code below I'm successfully changing the color
of the left and right arrows based on how far a div
(shadeFinder) has scrolled on the x axis. I want to add throttling to this code. Can someone explain how? I have the example from w3Schools below but finding it hard to merge it into my code.
ANSWER
Answered 2020-Jan-21 at 20:32The requestAnimationFrame
API takes a callback function that returns a timestamp (I'll use RAF as shorthand hereon). If you wanted to use JS to animate, you can use the timestamp to call cancelAnimationFrame
later. But to use just for throttling, you don't necessarily need to store this value, for now.
Since you already have a variable called last_known_scroll_position
scoped for used within doSomething
, you don't have to pass the scrollLeft
value to the RAF function, but instead pass the doSomething
as the callback after setting the last_known_scroll_position
to the current scrollLeft
value at the time of the scroll event.
That callback will receive a timestamp
parameter, but you don't need it. The callback can update the color based on a calculation from the last_known_scroll_position
.
See the example below.
Community Discussions, Code Snippets contain sources that include Stack Exchange Network
Vulnerabilities
No vulnerabilities reported
Install www.html5rocks.com
Support
Reuse Trending Solutions
Find, review, and download reusable Libraries, Code Snippets, Cloud APIs from over 650 million Knowledge Items
Find more librariesStay Updated
Subscribe to our newsletter for trending solutions and developer bootcamps
Share this Page