Display an Image Created by a Function on a p5.js Canvas

I'm attempting to import an image generated by a function in the gaborgen.js library.

The goal is to create a Gabor patch image using this library, save it as an image variable in p5, and then show it on the canvas.

The library has a gaborgen() function that requires two parameters. For example, calling gaborgen(50, 40) would produce a Gabor patch based on those values. The returned image from the library is in base64 PNG format.

My initial attempt to load the image was unsuccessful, resulting in just a blank screen in the sketch.

var img;

function setup() {
    createCanvas(640, 360);
    img = createImg(gaborgen(50, 40));
}

function draw(){
    background(0);
    image(img, 0, 0, img.elt.width, img.elt.height);
}

The gaborgen() function in the Gaborgen.js library looks like this:

gaborgen = function(tilt, sf) {
    // Function code here
};

Is there a way for me to successfully load a base64 PNG image returned by a function into p5.js?

Answer №1

If you want to incorporate a base-64 encoded image directly in P5.js, simply input the string into the loadImage() function. Here's an illustration:

let img;

function setup() {
  createCanvas(400, 400);
  img = loadImage('data:image/png;base64, iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg==');
}

function draw() {
  background(220);
    image(img, 0, 0, width, height);
}

Remember the crucial part of the argument is data:image/png;base64,.

If gaborgen() doesn't return what you expect, it's essential to debug and locate the problem area. Progress incrementally and use your developer tools to catch any errors.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Steps to activate an event when Windows is loaded

Every time windows load, I want to run $('select[name="order_id"]').change(), but it's not working as expected. After debugging in the browser console, I can see that the script $('select[name="order_id"]').cha ...

Vue.js dynamic routing is causing CSS to malfunction

Recently, I implemented a route in my Vue application with the following configuration: { path: '/coursedetailed/:id', component: MyCourseDetailed, props: true }, Everything seemed to be working fine until I navigated to the page and noti ...

IE6 does not support Ajax jquery functionality

Currently, I am facing an issue with my ajax jquery code. It seems to be working perfectly on all browsers except for IE6. I have tried making some changes but unfortunately, it did not solve the problem. I would greatly appreciate any help or suggestions ...

Remove all items from the Backbone Collection and delete them from the corresponding Lawnchair storage

I'm currently utilizing Backbone.js along with Lawnchair and backbone.lawnchair.js. My query pertains to the right approach for "emptying" a collection, both in the application itself and in localStorage. At present, I am implementing a method simil ...

Error message: AJAX encountered an unexpected token that caused a SyntaxError to be thrown

I have recently developed a user-friendly form that is supposed to fetch translation keys via the API. However, I encountered an issue that reads "Uncaught SyntaxError: Unexpected token :" when executing the code. Allow me to present you with a snippet of ...

Looking up the image directory in a JSON file on the fly - (Module not found...) React, Next.js

I'm attempting to dynamically retrieve data from a JSON file and insert it into a component. { "team": [ { "id": "", "name": "", "role": "", "bio": "", "major": "", "i ...

Incorporating OpenLayers and TypeScript: Issue with Event.map.forEachFeatureAtPixel, where the argument type is not compatible with the parameter type

I am currently attempting to implement Open Layers v7.2.2 with TypeScript. {When not using TypeScript, the code functions properly} function OnMapClick(event: MapBrowserEvent<UIEvent>) { event.map.forEachFeatureAtPixel(event.pixel, function(curren ...

Would it be feasible to rename files uploaded to Firebase within an AngularJS controller using logic?

Is there a way to apply regex to rename uploaded files before saving them to firebase storage? It seems that firebase file metadata does not support this function. I am currently using angularjs and would appreciate any guidance on this matter. ...

V-Calendar is not displaying the accurate dates

https://i.stack.imgur.com/zk4h7.png The image displays dates starting on June 1, 2022, which should be a Wednesday but appears as a Sunday on the calendar. This issue affects all months as they start on a Sunday instead of their respective weekdays. The p ...

What is the best way to add an event listener to every item in a list within a Vue component

Here is a component structure that I am working with: Vue.component('navbar', { props: ['navitem'], methods: { users: function () { //users code }, test:function(){ } }, template: '<li v-on:cl ...

There is an issue with the syntax in your for-loop control where a closing parenthesis is missing

When I try to navigate through the arrays from 1 to 4 in the given JSON data, Firebug shows me an error message: SyntaxError: missing ) after for-loop control [Break On This Error] for( x in LGIntake.corpCodeOptions.marketSegment.1){ StartI...aseId=0 ...

Error 500 - NodeJS API Updater Experiencing Technical Difficult

Time to bring in the experts because I'm stuck on this issue. I have a basic CRUD API built with Node and EJS on the front-end. The problem lies in the selectAllRecords view where I display a table of all records. Each record has an edit button tha ...

Saving an array within the Yii framework

The view contains the following form: <form method="POST" action="<?php echo Yii::$app->request->baseUrl;?>/telephone/addnow/" role="form" enctype="multipart/form-data"> <label>Upload your photo:</label><input type="fi ...

Can the arrangement of divs be altered solely based on specific browser widths, rather than just for visual appeal?

Imagine having the following HTML structure: <div class="outer"> <div class="inner"> </div> </div> However, under the condition @media (min-width: 890px) and (max-width: 958px), you want it to look like this: <div clas ...

Utilizing the onscroll feature to trigger a function within a ScrollView

I am facing an issue with an animated view where I need to execute multiple events within the onScroll property. The current onScroll implementation is as follows: onScroll={ Animated.event( [{ nativeEvent: { conten ...

Creating Helper Functions for Modifying Arrays in Javascript Without Creating a New Reference: How Can I Extend the Array Class?

This library is designed for managing user lists using socket.io. I've implemented custom methods in an Array extension class. However, I've encountered an issue with the removeUser method. While logging indicates that the user is successfully ...

JavaScript code to locate the most pertinent strings within an array based on a specific substring

I'm working with a large array containing names of people, such as: let names = [ "John Brown", "Tristan Black", "Carl Jobbs", "Aidan Burrows", "Taylor Joe" ]; When given an input, I want to return the top 5 most relevant results ...

Creating a node.js function that can be used with OracleDB

I'm currently delving into learning nodeJS, but I'm facing a roadblock and can't seem to figure out what's causing the issue. Even the Debugger isn't providing much help. Any assistance or guidance would be greatly appreciated. The ...

What is causing the consistent occurrences of receiving false in Angular?

findUser(id:number):boolean{ var bool :boolean =false this.companyService.query().subscribe((result)=>{ for (let i = 0; i < result.json.length; i++) { try { if( id == result.json[i].user.id) ...

What is the best way to organize two separate arrays based on a single shared variable?

I have two separate arrays containing information. One array includes the title of each national park and other related data, while the second array consists of alerts from the entire NPS system. The common factor between these arrays is the parkCode. How ...