Coder Social home page Coder Social logo

imgui-particle-background's Introduction

Particle background for imgui

always liked the look of these sort of background so thought id have a go at making one and think it has came out pretty good

Usage

Initializes the particles setting the x and y to random and sets the velocity which will be used later to set the position

  static bool initialized = false;
	if (!initialized)
	{
		for (int i = 0; i < numParticles; ++i)
		{
			particlePositions[i] = ImVec2(
				ImGui::GetWindowPos().x + ImGui::GetWindowSize().x * static_cast<float>(rand()) / RAND_MAX,
				ImGui::GetWindowPos().y + ImGui::GetWindowSize().y * static_cast<float>(rand()) / RAND_MAX
			);

			particleVelocities[i] = ImVec2(
				static_cast<float>((rand() % 11) - 5),
				static_cast<float>((rand() % 11) - 5)
			);

		}

		initialized = true;
	}

Draws the lines to the cursor and particles depending on the distance

  ImVec2 cursorPos = ImGui::GetIO().MousePos;
	for (int i = 0; i < numParticles; ++i)
	{
		//draw lines to particles
		for (int j = i + 1; j < numParticles; ++j)
		{
			float distance = std::hypotf(particlePositions[j].x - particlePositions[i].x, particlePositions[j].y - particlePositions[i].y);
			float opacity = 1.0f - (distance / 55.0f);  // opacity cahnge

			if (opacity > 0.0f)
			{
				ImU32 lineColor = ImGui::GetColorU32(ImVec4(1.0f, 1.0f, 1.0f, opacity));
				drawList->AddLine(particlePositions[i], particlePositions[j], lineColor);
			}
		}

		//draw lines to cursor
		float distanceToCursor = std::hypotf(cursorPos.x - particlePositions[i].x, cursorPos.y - particlePositions[i].y);
		float opacityToCursor = 1.0f - (distanceToCursor / 52.0f);  // Adjust the divisor to control the opacity change

		if (opacityToCursor > 0.0f)
		{
			ImU32 lineColorToCursor = ImGui::GetColorU32(ImVec4(1.0f, 1.0f, 1.0f, opacityToCursor));
			drawList->AddLine(cursorPos, particlePositions[i], lineColorToCursor);
		}
	}

Updates adn renders the particles setting the positions, keeping them in bound of the window and setting the colour

float deltaTime = ImGui::GetIO().DeltaTime;
	for (int i = 0; i < numParticles; ++i)
	{
		particlePositions[i].x += particleVelocities[i].x * deltaTime;
		particlePositions[i].y += particleVelocities[i].y * deltaTime;

		// Stay in window
		if (particlePositions[i].x < ImGui::GetWindowPos().x)
			particlePositions[i].x = ImGui::GetWindowPos().x + ImGui::GetWindowSize().x;
		else if (particlePositions[i].x > ImGui::GetWindowPos().x + ImGui::GetWindowSize().x)
			particlePositions[i].x = ImGui::GetWindowPos().x;

		if (particlePositions[i].y < ImGui::GetWindowPos().y)
			particlePositions[i].y = ImGui::GetWindowPos().y + ImGui::GetWindowSize().y;
		else if (particlePositions[i].y > ImGui::GetWindowPos().y + ImGui::GetWindowSize().y)
			particlePositions[i].y = ImGui::GetWindowPos().y;

		ImU32 particleColour = ImGui::ColorConvertFloat4ToU32(settings::particleColour);

		//render particles behind components
		drawList->AddCircleFilled(particlePositions[i], 1.5f, particleColour);
	}
credits to cazzwastaken for the base of the imgui: https://github.com/cazzwastaken/borderless-imgui-window

imgui-particle-background's People

Contributors

dannybanno avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.