Javascript Circle Stroke

Exploring the concept of drawing a line through a circle

Example image: https://i.sstatic.net/I8B60.jpg

Check out my code snippet: https://codepen.io/ethan-horrigan/pen/OrRLXx

    var r = 200;
    var x1 = 800 / 2;
    var y1 = 540 / 2;

    var x = r * Math.cos(Math.PI / 180 * 135) + x1;
    var y = r * Math.sin(Math.PI / 180 * 315) + y1;

    ctx.beginPath();
    ctx.arc(x1, y1, r, 0, Math.PI * 2, false);

    ctx.moveTo(x1,y1);
    ctx.lineTo(x, y);
    ctx.stroke();

Answer №1

I think this is the solution you're looking for:

ctx.moveTo(x, y);
ctx.lineTo(x1 + (x1 - x), y1 + (y1 - y));
ctx.stroke();

Check out the code on Codepen

Answer №2

let radius = 200;
let centerX = 800 / 2;
let centerY = 540 / 2;

let xCoord = radius * Math.cos(Math.PI * .75) + centerX;
let yCoord = radius * Math.sin(Math.PI * 1.75) + centerY;
context.beginPath();
context.arc(centerX, centerY, radius, Math.PI * .25, Math.PI * 2.25, false);

context.lineTo(xCoord, yCoord);
context.stroke();

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 incorporate the controller directive specified within a closure into the testing specification

I followed the recommendation in option 2 of the accepted answer to a question on stackoverflow about unit testing directive controllers in Angular without making the controller global. You can find the original post here. Here is a snippet of my code: ( ...

Using JavaScript to make an AJAX request when a dropdown menu from

Looking to update a graph created with Google Chart API using a drop-down menu. Currently facing a few issues. The dropdown is sending a request to the wrong URL. Instead of /graph/, it's sending data to the current page, which is /services/. This ...

Issue occurs when trying to access the 'set' property of an undefined variable, leading to an error message stating "Cannot read property 'set' of undefined" while using 'this

I'm facing an issue while setting up basic cookies for my Vue project. When I try to set a cookie, I encounter the following error. My package.json file indicates that I am using vue-cookies version ^1.7.4. The error message occurs when I click the bu ...

Utilize web workers for efficient processing of a limited number of files simultaneously in JavaScript

Utilizing the web worker concept for file uploads has resulted in creating a web worker for each selected file. The idea now is to optimize this process by creating 5 web worker threads to handle the first batch of five files, terminating them afterwards b ...

Despite seeming false, my Javascript if statement still evaluates to true

On my webpage, I have a button and a form. The intended functionality is for the button to display the form when clicked, and hide it when clicked again. The issue I'm facing is that no matter if the statement is true or false, the first if statement ...

When the chart is zoomed in, Highcharts displays points outside the chart area when hovering over the line

I recently began working with highcharts and for my current project, I need a prototype chart that allows zooming in and out. Specifically, I want to display the y-axis value in the tooltip only when the user hovers directly over the points. However, in hi ...

Utilizing dynamic IDs in JavaScript for HTML elements: A comprehensive guide

I am in the process of dynamically creating a drop-down box. While I have successfully created dynamic drop-down boxes, I now need to populate them with values from a database. To achieve this, I believe I need to use an on-click function. Despite attemp ...

Comparing AngularJS $interpolate with $filter

AngularJS offers different tools for manipulating data displayed to users. While $filter is commonly used for formatting data, $interpolate enables real-time updates within a text string. Do $interpolate and $filter have any connection? How do they differ ...

Neglecting to include an HTTP header

I am attempting to create an HTTP request with an Authorization header: $.ajax({ type: 'GET', url: "https://subdomain.domain.com/login", beforeSend: function (xhr) { xhr.setRequestHeader ("Auth ...

What is the most effective method for integrating an HTML template into AngularJS without using an iframe?

I am currently working on a project where I need to incorporate different templates into the body section for previewing purposes. While it is commonly achieved using IFrames, I have encountered technical challenges that prevent me from taking that route. ...

Creating a concatenated multidimensional array can be achieved by combining multiple arrays of varying lengths into a single multidimensional array

Is there a way to transform array a into array b? Alternatively, how can I create a matrix-style multidimensional array using the values from array a below? SPECIFICATIONS: 1. The length of array a is variable. 2. Each element in array a has a variable l ...

Is it necessary to ensure application readiness before proceeding with unit testing exports?

I've been facing a challenge while trying to utilize Jest for unit testing an express API that I've developed. The issue arises when the database needs to be ready before running the test, which doesn't seem to happen seamlessly. In my serve ...

Guide on saving the token in local storage or cookies to grant access to specific web pages for users

Currently, I am in the process of developing an authentication system using Node.js, MySQL, and Express. Initially, I focused on saving and verifying user credentials in the database. Recently, I incorporated JWT (JSON Web Token) into the system. My next s ...

How can I handle the issue of the body background overlapping with the modal window?

Recently, I created a dialog box using the code below: $(function(){ $('#saveandcontinue').click(function(){ $('body').css('background', '#999'); $('.footer').css('background&a ...

Iteratively modify each essential attribute of a JSON object

In my data set, I have moisture levels recorded at various timestamps in a JSON object: { "values": { "21-Aug-2020 20:28:06:611591": "58.59", "21-Aug-2020 20:28:09:615714": "71.42", "21-A ...

How should a JavaScript object be properly formatted?

Recently, I created a project using ng-repeat with AngularJS 1.x, and it was smooth sailing. JavaScript: var app = angular.module('myModule', []); app.controller('myController', function() { this.dishes = [ { 'name&a ...

Removing a specific object from a MongoDB array by its unique identifier

I'm currently developing an application that focuses on playlists. Utilizing MongoDB with mongoose, I am storing videos within each playlist using an array structure as demonstrated below: { _id: ObjectId("61ca1d0ddb66b3c5ff93df9c"), name: "Playli ...

Creating an Interactive Menu System with Nested Options in Angular 2

I have a JSON structure that I need to transform into a menu with sub-menus based on the value of the sub-menu_location field. Here's an example: { "data": [ { "menu_name": "Primary Operations", "enabled": true, "sub-menu_lo ...

Is recursion effective in this scenario? (javascript/node.js)

Currently, I am working on creating a TV using a Raspberry Pi and JavaScript to play the same playlist repeatedly. Although playing the files is not an issue, I am facing a challenge with the asynchronous nature of JavaScript. Below is the code that is cau ...

Create a standard chart with a colored map using Three.js

Recently, I dove into the world of Three.js and found myself on a mission to convert a color map into a normal map. My goal is to create a normal map similar to [image 2], based on the color map shown in [image 1]. Instead of simply uploading files, I wa ...